⚙️ systemd basics
systemd starts and watches everything on a modern Linux system: services, timers, mounts, and the logs. Two commands do almost everything: systemctl and journalctl.
Services with systemctl
systemctl status nginx # is it running? last log lines
sudo systemctl start nginx # start now
sudo systemctl stop nginx # stop now
sudo systemctl restart nginx # stop + start
sudo systemctl reload nginx # re-read the config without stopping (if supported)
sudo systemctl enable --now nginx # start now AND at every boot
sudo systemctl disable --now nginx # stop now and don't start at boot
systemctl is-enabled nginx # enabled / disabled
systemctl --failed # everything that went wrong
systemctl list-units --type=service # all loaded services
Change a setting of a service without editing the original file (it is overwritten by updates):
sudo systemctl edit nginx # opens an "override" file
systemctl cat nginx # the unit file plus all overrides
sudo systemctl daemon-reload # after changing unit files by hand
Logs with journalctl
journalctl -u nginx # all logs of one service
journalctl -u nginx -f # follow live (like tail -f)
journalctl -u nginx --since "1 hour ago"
journalctl -b # everything since the last boot
journalctl -b -1 -p err # errors from the boot before (after a crash)
journalctl -k # kernel messages (like dmesg)
journalctl -e # jump to the end
journalctl --disk-usage # how big are the logs?
sudo journalctl --vacuum-time=2weeks # remove logs older than two weeks
Your own service
A small program or script that should always run, and restart when it crashes. sudo nano /etc/systemd/system/myapp.service:
[Unit]
Description=My little app
After=network-online.target
Wants=network-online.target
[Service]
User=myapp
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/run.sh
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
journalctl -u myapp -f
For your own user (no root needed), put the file in ~/.config/systemd/user/ and add --user to every command: systemctl --user enable --now myapp. User services only run while you are logged in, unless you run loginctl enable-linger $USER once.
Timers instead of cron
A timer starts a service at set times. Compared to cron you get logging in the journal, catching up after the computer was off, and a random delay so not everything starts at once.
/etc/systemd/system/cleanup.service:
[Unit]
Description=Clean up old files
[Service]
Type=oneshot
ExecStart=/usr/bin/find /srv/tmp -type f -mtime +7 -delete
/etc/systemd/system/cleanup.timer (same name, .timer):
[Unit]
Description=Clean up old files every night
[Timer]
OnCalendar=*-*-* 03:00
Persistent=true
RandomizedDelaySec=15m
[Install]
WantedBy=timers.target
sudo systemctl enable --now cleanup.timer
systemctl list-timers # all timers, last and next run
sudo systemctl start cleanup.service # run it right now, to test
Not sure about an OnCalendar value? Let systemd explain it:
systemd-analyze calendar "Mon..Fri *-*-* 09:00"
systemd-analyze calendar weekly
A real-life example: daily backups with restic.
Why does my computer boot so slowly?
systemd-analyze # total boot time
systemd-analyze blame | head # the slowest services