Contrôlez des services systemd et inspectez leurs journaux.
objectifs d’apprentissage
- Activer/démarrer un service.
- Consulter journaux.
- Créer un unit simple.
prérequis
- Système avec systemd.
- Accès sudo.
notions clés
- systemctl.
- journalctl.
- unit files, WantedBy.
démonstration guidée
étape 1
Gestion de services existants.
systemctl status ssh || true
sudo systemctl enable --now cron || true
étape 2
Créer un service simple hello.service.
printf '%s
' '[Unit]' 'Description=Hello' '[Service]' 'ExecStart=/bin/sh -c "echo hello && sleep 30"' 'Restart=on-failure' '[Install]' 'WantedBy=multi-user.target' | sudo tee /etc/systemd/system/hello.service >/dev/null
sudo systemctl daemon-reload
sudo systemctl enable --now hello.service
journalctl -u hello.service -n 5 --no-pager || true
exercice
Ajoutez RestartSec=5s et observez le redémarrage après un échec.
correction
Paramètre de délai.
sudo sed -i 's/^Restart=.*/Restart=always/' /etc/systemd/system/hello.service
echo 'RestartSec=5s' | sudo tee -a /etc/systemd/system/hello.service
sudo systemctl daemon-reload && sudo systemctl restart hello.service
quiz éclair
- Quelle commande recharge les units après modification ?
- a) systemctl daemon-reload
- b) systemctl reload
- c) systemctl enable