systemd Introduction

systemd manages everything that runs on modern Linux: services, mounts, timers, and more. systemctl is how you control it.

Basic Service Management

Terminal
$# Check service status
$systemctl status nginx
โ— nginx.service - A high performance web server Active: active (running) since Mon 2025-01-14 10:00:00 UTC Main PID: 1234 (nginx)
$
$# Start a service
$sudo systemctl start nginx
$
$# Stop a service
$sudo systemctl stop nginx
$
$# Restart a service
$sudo systemctl restart nginx
$
$# Reload config without restart
$sudo systemctl reload nginx

Enable/Disable on Boot

Terminal
$# Start on boot
$sudo systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service
$
$# Don't start on boot
$sudo systemctl disable nginx
$
$# Enable AND start now
$sudo systemctl enable --now nginx

Check Status

Terminal
$# Is it running?
$systemctl is-active nginx
active
$
$# Will it start on boot?
$systemctl is-enabled nginx
enabled
$
$# Is it failed?
$systemctl is-failed nginx
active

List Services

Terminal
$# All loaded services
$systemctl list-units --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION cron.service loaded active running Regular background program... nginx.service loaded active running A high performance web server sshd.service loaded active running OpenSSH server daemon
$
$# Failed services only
$systemctl --failed

View Logs

Terminal
$# Logs for a service
$journalctl -u nginx
(all nginx logs)
$
$# Follow logs (like tail -f)
$journalctl -u nginx -f
(live log output)
$
$# Last 50 lines
$journalctl -u nginx -n 50
$
$# Since last boot
$journalctl -u nginx -b

journalctl is Powerful

journalctl replaces reading log files manually. It can filter by time, service, priority, and more.

Create a Service

Create /etc/systemd/system/myapp.service:

hljs ini
[Unit]
Description=My Application
After=network.target

[Service]
Type=simple
User=deploy
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/server
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Then:

Terminal
$sudo systemctl daemon-reload
(reload unit files)
$sudo systemctl enable --now myapp
(enable and start)

Service Types

TypeBehavior
simpleProcess in ExecStart is the service
forkingProcess forks, parent exits
oneshotRun once, then exit
notifyService signals when ready

Restart Policies

RestartWhen
noNever restart
alwaysAlways restart
on-failureOnly if failed
on-abnormalAbnormal exit

Reload After Changes

Terminal
$# After editing a unit file
$sudo systemctl daemon-reload
$sudo systemctl restart myapp

System State

Terminal
$# Reboot
$sudo systemctl reboot
$
$# Shutdown
$sudo systemctl poweroff
$
$# Suspend
$sudo systemctl suspend

Practical: Node.js Service

hljs ini
# /etc/systemd/system/nodeapp.service
[Unit]
Description=Node.js Application
After=network.target

[Service]
Type=simple
User=deploy
Group=deploy
WorkingDirectory=/var/www/app
Environment=NODE_ENV=production
Environment=PORT=3000
ExecStart=/usr/bin/node server.js
Restart=on-failure
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodeapp

[Install]
WantedBy=multi-user.target
Terminal
$sudo systemctl daemon-reload
$sudo systemctl enable --now nodeapp
$sudo systemctl status nodeapp
โ— nodeapp.service - Node.js Application Active: active (running)
Knowledge Check

What does 'systemctl enable nginx' do?

Quick Reference

CommandPurpose
systemctl start svcStart service
systemctl stop svcStop service
systemctl restart svcRestart service
systemctl status svcCheck status
systemctl enable svcStart on boot
systemctl disable svcDon't start on boot
journalctl -u svcView service logs
systemctl daemon-reloadReload unit files

Key Takeaways

  • systemctl controls services
  • start/stop/restart for immediate control
  • enable/disable for boot behavior
  • journalctl -u service for logs
  • Create custom services in /etc/systemd/system/
  • Always daemon-reload after editing unit files

Next: environment variables and shell configuration.