Linux

Understanding systemd: Services, Timers, and Journalctl

ยท1 min read

Systemd is the init system and service manager used by most modern Linux distributions. Despite controversy in the Linux community, systemd is here to stay. Understanding how it works is essential for any Linux system administrator or developer deploying services on Linux.

Service Units

Systemd service units define how daemons are started, stopped, and managed. A basic service file includes [Unit], [Service], and [Install] sections. The service section defines the ExecStart command, restart policy, user/group, and resource limits.

[Unit]
Description=My Custom Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/my-app
Restart=on-failure
User=myapp

[Install]
WantedBy=multi-user.target

Timer Units

Systemd timers replace traditional cron jobs with more powerful scheduling. They support calendar events, monotonic timers (relative to boot), and randomized delays to prevent thundering herd problems. Timer units pair a .timer file with a matching .service file.

Journalctl and Logging

Journald collects and manages system logs. Journalctl queries logs with filters by service, priority, time range, and boot session. Commands like journalctl -u nginx.service --since today provide quick access to specific service logs.

Troubleshooting

Failed services can be diagnosed with systemctl status, journalctl -xe, and analyzing dependency chains. Common issues include incorrect ExecStart paths, permission problems, and missing environment variables.