Affichage des articles dont le libellé est linux. Afficher tous les articles
Affichage des articles dont le libellé est linux. Afficher tous les articles

jeudi 28 août 2025

Systemd healtcheck with side service and monotonic timer, auto-healing

What : bypass the lack of healthcheck of systemd

  • systemd service "what-service.service"
  • systemd timer  "what-service-healthcheck.timer"
    • triggers a systemd service "what-service-healthcheck.service"
       which lanches a script "
      service_health_check.sh"
    • script that :
      • curl's heal-tcheck URL "HEALTH_CHECK_URL"
      • if KO, restart the targetted service



what-service-healthcheck.timer

[Unit]
Description=Run health check every 15 seconds
[Timer]
# Wait 1 minute after boot before the first check
OnBootSec=1min
# Run the check 15 seconds after the last time it finished
OnUnitActiveSec=15s
[Install]
WantedBy=timers.target


By default the timer service will trigger the unit service with the same name, no need to specify it.

what-service-healthcheck.service.j2
[Unit]
Description=Health Check for {{ what_service }}
Requires={{ what_service }}.service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/service_health_check.sh
Restart=on-failure
OnFailure={{ what_service }}


service_health_check.sh 
#!/bin/bash
# The health check endpoint
HEALTH_CHECK_URL="http://localhost:{{ running_port }}/health_check"
# Use curl to check the endpoint.
# --fail: Makes curl exit with a non-zero status code on server errors (4xx or 5xx).
# --silent: Hides the progress meter.
# --output /dev/null: Discards the response body.
if ! curl --silent --fail --max-time 2 --output /dev/null "$HEALTH_CHECK_URL"; then
echo "Health check failed for {{ service_name }}. Restarting..."
# Restart is performed on failure from healthcheck service
exit 1
fi



Adding through ansible (to do : fix indentation, blog isn't great for this)

<role>/tasks/main.yml
--- 

- name: Generate what-service systemd file
ansible.builtin.template:
src: what-service.service.j2
dest: /etc/systemd/system/what-service.service
mode: "0755"
notify: Restart what-service

 - name: Copy the health check script
ansible.builtin.copy:
src: service_health_check.sh
dest: /usr/local/bin/service_health_check.sh
owner: root
group: root
mode: '0755'
vars: 
  service_name: what-service

- name: Copy the health check systemd service file
ansible.builtin.copy:
src: what-service-healthcheck.service
dest: /etc/systemd/system/what-service-healthcheck.service
owner: root
group: root
mode: '0644'
notify: Reload systemd

- name: Copy the health check systemd timer file
ansible.builtin.copy:
src: what-service-healthcheck.timer
dest: /etc/systemd/system/what-service-healthcheck.timer
owner: root
group: root
mode: '0644'
notify: Reload systemd

- name: Enable and start the health check timer
ansible.builtin.systemd:
name: healthcheck.timer
state: started
enabled: yes
daemon_reload: yes # Ensures systemd is reloaded before starting


<role>/handlers/main.yml
---
- name: Restart what-service 
ansible.builtin.service:
name: what-service
state: restarted
daemon_reload: true

- name: Reload systemd
ansible.builtin.service:
daemon_reload: yes

- name: Restart what-service-healthcheck.timer
ansible.builtin.service:
name: what-service-healthcheck.timer
state: restarted
daemon_reload: true

mardi 19 novembre 2024

removing unwanted *typo* systemd instance'd template units

 Systemd has this great feature : 


file: foo@.service 

contains the definition, as usual, with "%i"

=> will replace anything in the service file 

for example:


systemctl start foo@bar.service

systemctl start foo@resto.service


=> will start  service foo@bar  and foo@resto


but if resto does not make a valid service, it fails and can't be easily removed from the list of apparently formerly existing services.

(cf. for example https://bbs.archlinux.org/viewtopic.php?id=286912&p=1 )


As you can see with the command : 

 systemctl list-units --type=service | grep 'foo'

  UNIT                                                                                      LOAD   ACTIVE SUB     DESCRIPTION

  foo@alpha.service                                                               loaded active running Foo for protocol alpha

● foo@lish.service                                                              masked failed failed  foo@lish.service



=> Solution (on ubuntu, but no reason it does not work elsewhere) 

 systemctl mask foo@resto.service #this creates a symlink / replaces the symlink with one pointing to /dev/null, and marks the service as failed => no restart will happen again

 systemctl reset-failed # cleans failed services




mardi 9 novembre 2021

Linux cleaning tips (systemd, apt/ubuntu, docker)

 (evolutive article where I store what's been useful to me to clean space on linux servers I'm using)

Note : my current servers are running Ubuntu.



OS / SystemD : purge logs bigger than ...

Only keep 10M of each log files handle through syslog

sudo journalctl --vacuum-size 10M


OS / apt : remove remnants

Remove automatically all unused packages Remove automatically installed packages not needed anymore (i.e. packages that were installed as dependencies of packages that have now been removed or aren't requiring this dependency anymore)

apt options :

  •   autoremove - Remove automatically all unused packages
  •   clean - Erase downloaded archive files
  •   autoclean - Erase old downloaded archive files

sudo apt-get clean autoremove autoclean



Docker : remove logs older than ...

Only keep the last 1000h of docker containers logs

sudo docker image prune --all --filter "until=1000h" -f


Docker : remove remnants

Be careful ! If your docker environments are not running they might be deleted by some of the following commands !

Remove unused volumes, containers, images, networks, etc.

sudo docker system prune -f 

sudo docker system prune -a --volumes


dimanche 10 octobre 2010

(links) For my camera


AVCHD Videos

I've got a Panasonic DMC-ZX3 since a few month and I found a few informations on how to view, convert, etc. the videos it takes. They are in HD and outputed in a strange .MTS files (and some others, I'll write more on that some time later). Regular players can't read those files, and I eventually found some converting tools under linux (I'm using ubuntu)


AVCHD = sudo apt-get install libavcodec-extra-52 winff openshot
http://blog.yann.info/2010/05/16/avchd-pour-les-nuls-et-les-pauvres/





Tethering (using computer as a screen for the camera while shooting)

Meanwhile, I found this website explaining how to use your computer screen for your camera. I didn't test already, but I like the idea.


http://grahambinns.com/blog/2010/04/18/gphoto2-f-spot-win/

dimanche 23 août 2009

live network trafic view ? iptraf, iftop

A explorer

http://www.commentcamarche.net/faq/sujet-848-linux-trafic-entrant-sortant-en-temps-reel


On trouvera ci dessous les extraits des deux manpages de iptraf et ethstatus.
Intéressant pour comptabiliser la quantité de données qui circulent sur les différentes interfaces, ainsi que pour monitorer quelles sont les connexions ouvertes.

Après un bref test, "ethstatus" m'a cependant l'air d'utiliser beaucoup de ressources CPU, et semble être surtout un reformatage des infos d'ifconfig. Peu intéressant donc.

Au contraire, "iptraf" était plus léger et apportait plus d'informations.

Egalement, iftop fourni plus d'informations et permet de lister les connextions courantes un peu comme "top" le ferait.






IPTRAF(8) IPTRAF(8)

NAME
iptraf - Interactive Colorful IP LAN Monitor

SYNOPSIS
iptraf { [ -f ] [ -q ] [ -u ] [ { -i iface | -g | -d iface | -s iface |
-z iface | -l iface } [ -t timeout ] [ -B [ -L logfile ] ] ] | [ -h ] }

DESCRIPTION
iptraf is an ncurses-based IP LAN monitor that generates various net‐
work statistics including TCP info, UDP counts, ICMP and OSPF informa‐
tion, Ethernet load info, node stats, IP checksum errors, and others.

If the command is issued without any command-line options, the program
comes up in interactive mode, with the various facilities accessed
through the main menu.



IFTOP(8) IFTOP(8)

NAME
iftop - display bandwidth usage on an interface by host

SYNOPSIS
iftop -h | [-nNpbBP] [-i interface] [-f filter code] [-F net/mask]

DESCRIPTION
iftop listens to network traffic on a named interface, or on the first
interface it can find which looks like an external interface if none is
specified, and displays a table of current bandwidth usage by pairs of
hosts. iftop must be run with sufficient permissions to monitor all
network traffic on the interface; see pcap(3) for more information, but
on most systems this means that it must be run as root.

By default, iftop will look up the hostnames associated with addresses
it finds in packets. This can cause substantial traffic of itself, and
may result in a confusing display. You may wish to suppress display of
DNS traffic by using filter code such as not port domain, or switch it
off entirely, by using the -n option or by pressing R when the program
is running.

By default, iftop counts all IP packets that pass through the filter,
and the direction of the packet is determined according to the direc‐
tion the packet is moving across the interface. Using the -F option it
is possible to get iftop to show packets entering and leaving a given
network. For example, iftop -F 10.0.0.0/255.0.0.0 will analyse packets
flowing in and out of the 10.* network.

mardi 2 juin 2009

Rubber : a LaTeX processing tool

I still have to test it with other .tex files, but I just discovered and tried "rubber", and LaTeX compilation seems to be pretty fast (for an 8 pages document).


It seems that it proposes a tool that compiles and analyses the LaTeX source file to generate either a ps, pdf or dvi, and to run the needed tools : makeindex, bibtex, etc. and all by itself without having to write a complicated makefile.



From "rubber" man pages :

NAME
rubber - a building system for LaTeX documents

SYNOPSIS
rubber [options] sources ...
rubber-pipe [options]

DESCRIPTION
Rubber is a wrapper for LaTeX and companion programs. Its purpose is,
given a LaTeX source to process, to compile it enough times to resolve
all references, possibly running satellite programs such as BibTeX,
makeindex, Metapost, etc. to produce appropriate data files.

The command rubber builds the specified documents completely. The
source files may be either LaTeX sources (in which case the suffix .tex
may be omitted) or documents in a format Rubber knows how to translate
into LaTeX (this currently means CWEB or Literate Haskell documents).
If one compilation fails, the whole process stops, including the compi‐
lation of the next documents on the command line, and rubber returns a
non-zero exit code.


Some Links :

mardi 21 avril 2009

date relative

Utilisation de la commande date pour calculer une date relative en nombre d'heures à partir d'une autre date.

  1. 1ère étape : entrer la date initiale avec date
    $ date --date='2009-03-23 12:00'


  2. 2ème étape : utiliser le résultat obtenu comme base pour y ajouter un décalage en nombre d'heures, soit donc :
    $ date --date="$(date --date='2009-03-23 12:00') +1001 hours"
    Mon May 4 06:00:00 CEST 2009



mercredi 4 mars 2009

commandlinefu

Fantastique !

http://www.commandlinefu.com

c'était un peu l'idée de ce blog au départ, bien que cela ait un peu dévié...

En se retrouve sur ce site avec un équivalent twitter, mais uniquement pour des lignes de commande !

exemple, cette commande que j'avais eu l'occasion de chercher un jour pour une vidéo prise avec mon appareil photo...
mencoder -vf rotate=1 -ovc lavc -oac copy "$1" -o "$1"-rot.avi

jeudi 11 décembre 2008

distribution-specific information

Example :

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 8.04.1
Release: 8.04
Codename: hardy




lsb_release(1) lsb_release(1)

NAME
lsb_release - print distribution-specific information

SYNOPSIS
lsb_release [options]

DESCRIPTION
The lsb_release command provides certain LSB (Linux Standard Base) and
distribution-specific information.

If no options are given, the -v option is assumed.