Managing services with systemd
Most modern distributions use systemd to start, stop and supervise background services.
The everyday commands
systemctl status ssh
sudo systemctl restart ssh
sudo systemctl enable --now cron
systemctl list-units --type=service --state=failed
Reading logs
systemd collects the output of every service in the journal. journalctl -u ssh --since today shows today's messages for one unit, and journalctl -f follows everything live.
Writing a unit
A minimal service file lives in /etc/systemd/system/hello.service:
[Unit]
Description=Hello service
[Service]
ExecStart=/usr/local/bin/hello
Restart=on-failure
[Install]
WantedBy=multi-user.targetAfter creating or editing it, run sudo systemctl daemon-reload.
Timers instead of cron
A .timer unit can replace a cron entry and gives you logging, dependencies and randomised delays for free.