Services with systemd
Run, supervise, schedule and log long-running services on modern Linux using systemd units and journald.
Intermediate · about 6 hours · difficulty 2/5
Learn first: Processes, Signals and Resources, Users, Groups and Permissions
systemd is the init system on almost every mainstream distribution. It starts services at boot, restarts them when they crash, orders dependencies, schedules jobs and collects logs. If you deploy anything to a VM, you will write unit files.
Working with units
systemctl start|stop|restart|reload|status <unit>,enableto start at boot,daemon-reloadafter editing files.- Unit files live in
/etc/systemd/system/. A service unit has[Unit](description,After=,Wants=),[Service](ExecStart,User,Restart=on-failure,Environment/EnvironmentFile) and[Install](WantedBy=multi-user.target). - Override vendor units safely with
systemctl edit(drop-in files) instead of editing the originals.
Logs and timers
journalctl -u myapp -f,--since "10 min ago",-p errto filter by priority.- Timers replace cron for scheduled jobs, with logging and missed-run handling built in.
Hardening
Directives like NoNewPrivileges=true, ProtectSystem=strict, PrivateTmp=true and resource limits (MemoryMax, CPUQuota) sandbox a service cheaply.
Pitfalls
Forgetting daemon-reload, services that fork into the background when systemd expects Type=simple, and restart loops that hide a crash (check systemctl status and the journal).
Good enough to move on: you can turn any app into a hardened systemd service that restarts on failure, and schedule a job with a timer.
Key concepts
- systemctl basics
- Writing service unit files
- Restart policies and dependencies
- journalctl
- systemd timers vs cron
- Drop-in overrides
- Sandboxing directives
Mini project
Deploy a small web app as a systemd service running under its own user with Restart=on-failure and an EnvironmentFile, then add a timer that hits its health endpoint every 5 minutes and logs failures.
Check yourself
You edited a unit file and restarted the service but nothing changed. What step did you miss?
Free resources
- systemd project (systemd.io)
- systemd man pages (freedesktop.org)