/usr/lib/systemd/system/ (package-managed)/etc/systemd/system//etc/systemd/system/<unit>.d/*.conf~/.config/systemd/user/| Task | Command |
|---|---|
| Start / Stop / Restart | sudo systemctl start|stop|restart name.service |
| Enable / Disable | sudo systemctl enable|disable name.service |
| Status | systemctl status name.service |
| Tail logs | journalctl -u name.service -f |
| List failed units | systemctl --failed |
| Reload units | sudo systemctl daemon-reload |
| List timers | systemctl list-timers --all |
| Need | Command |
|---|---|
| Current boot only | journalctl -b |
| Previous boot | journalctl -b -1 |
| By priority | journalctl -p warning |
| Service since 1h | journalctl -u nginx --since \"1 hour ago\" |
| Follow service | journalctl -u nginx -f |
| Kernel messages | journalctl -k -b |
# /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
# /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.
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;'
ProtectSystem=strict, ProtectHome=yesNoNewPrivileges=yes, PrivateTmp=yesReadWritePaths=/var/lib/myappCapabilityBoundingSet= to drop caps