systemd and journalctl cheatsheet
systemd and journalctl: Unit files and timers
9 common systemd and journalctl tasks.
Write a service unit
A service that runs an app as its own user and restarts it on failure. Save it as /etc/systemd/system/myapp.service.
[Unit]
Description=My app
After=network-online.target
Wants=network-online.target
[Service]
User=myapp
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/myapp
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Reload unit files
Make systemd re-read unit files after you create or change one.
sudo systemctl daemon-reload
Override a unit safely
Create a drop-in override file instead of editing the packaged unit.
sudo systemctl edit nginx
Write a timer
Run a service every day at 03:00, and catch up after downtime. Pair it with a myjob.service.
[Unit]
Description=Run myjob daily
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
[Install]
WantedBy=timers.target
Check a unit file for errors
Check a unit file for mistakes.
systemd-analyze verify /etc/systemd/system/myapp.service
Test a calendar expression
Show when an OnCalendar expression will next fire.
systemd-analyze calendar "Mon..Fri 09:00"
Keep user services running after logout
Let a user's services start at boot and keep running without a login session.
sudo loginctl enable-linger myuser