Systemd is the init system on most modern distributions. It provides a unified way to manage processes, services, and logs, replacing older SysVinit and Upstart. Compared to simple init scripts, it offers declarative configuration, automatic dependency resolution, parallel service startup, and advanced resource control. Understanding systemd is essential for anyone managing Linux servers.
Basic Commands¶
systemctl start|stop|restart|reload|status nginx
systemctl enable|disable nginx
systemctl list-units --type=service --state=failed
systemctl list-timers # systemd timers (cron replacement)
The systemctl status command shows service state including recent log lines, PID, and uptime. For more detailed diagnostics, use journalctl -u service.
Custom Service¶
[Unit]
Description=My Application
After=network.target postgresql.service
[Service]
Type=simple
User=app
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/server --port 8080
Restart=on-failure
RestartSec=5
EnvironmentFile=/opt/myapp/.env
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now myapp
The [Unit] section defines dependencies — After ensures correct startup order, Requires creates a hard dependency. [Service] configures execution: Type=simple for direct startup, Type=forking for daemons, Type=notify for services that signal readiness. Restart=on-failure automatically restarts on failure, RestartSec sets the delay between attempts.
Resource Limits¶
[Service]
CPUQuota=200%
MemoryMax=2G
LimitNOFILE=65535
ProtectSystem=strict
PrivateTmp=yes
NoNewPrivileges=yes
Systemd cgroups limits isolate services and prevent one service from consuming all system resources. ProtectSystem=strict ensures read-only access to system directories, PrivateTmp isolates /tmp for each service. These security directives are recommended for production deployments.
Debugging¶
systemctl status myapp -l # complete status with logs
journalctl -u myapp --since '5 min ago' # logs from last 5 minutes
systemd-analyze blame # what slows down boot
systemd-analyze critical-chain myapp # dependency chain
Systemd Is the Standard¶
Learn unit files, dependencies, and resource limits. Investing in systemd knowledge pays off with every deployment and troubleshooting session.