systemd reference

unit locations

common commands

TaskCommand
Start / Stop / Restartsudo systemctl start|stop|restart name.service
Enable / Disablesudo systemctl enable|disable name.service
Statussystemctl status name.service
Tail logsjournalctl -u name.service -f
List failed unitssystemctl --failed
Reload unitssudo systemctl daemon-reload
List timerssystemctl list-timers --all

quick logging filters

NeedCommand
Current boot onlyjournalctl -b
Previous bootjournalctl -b -1
By priorityjournalctl -p warning
Service since 1hjournalctl -u nginx --since \"1 hour ago\"
Follow servicejournalctl -u nginx -f
Kernel messagesjournalctl -k -b

create a simple service

# /etc/systemd/system/myapp.service
[Unit]
Description=My App
After=network.target

[Service]
ExecStart=/usr/local/bin/myapp --flag
User=myuser
Group=myuser
Restart=on-failure
RestartSec=5
Environment=ENV=prod

[Install]
WantedBy=multi-user.target

Then:

sudo systemctl daemon-reload
sudo systemctl enable --now myapp.service

timer example (cron replacement)

# /etc/systemd/system/backup.service
[Unit]
Description=Nightly backup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh

# /etc/systemd/system/backup.timer
[Unit]
Description=Run backup daily

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

Enable with sudo systemctl enable --now backup.timer.

overrides without editing vendor unit

sudo systemctl edit nginx.service
# adds /etc/systemd/system/nginx.service.d/override.conf
[Service]
Environment=\"NODE_ENV=prod\"
ExecStart=
ExecStart=/usr/sbin/nginx -g 'daemon off;'

service sandboxing knobs

Return to Home