| name | systemd |
| description | Guide for systemd init system and service management. Use when creating, managing, or debugging system services, timers, and targets. Covers unit files, service lifecycle, dependencies, and journalctl logging. |
systemd Service Management
Unit File Basics
Service files location: /etc/systemd/system/ or /usr/lib/systemd/system/
# /etc/systemd/system/myservice.service
[Unit]
Description=My Application Service
After=network.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/bin/myapp
ExecStop=/usr/bin/myapp --stop
Restart=on-failure
RestartSec=5
User=myuser
Group=mygroup
WorkingDirectory=/opt/myapp
[Install]
WantedBy=multi-user.target
Service Types
| Type |
Description |
simple |
Process started by ExecStart is main process |
forking |
Process forks, parent exits (use PIDFile=) |
oneshot |
Process exits after completion |
notify |
Process sends sd_notify() when ready |
dbus |
Ready when D-Bus name acquired |
Service Management
# Start/stop/restart
systemctl start myservice
systemctl stop myservice
systemctl restart myservice
systemctl reload myservice
# Enable/disable at boot
systemctl enable myservice
systemctl disable myservice
# Check status
systemctl status myservice
systemctl is-active myservice
systemctl is-enabled myservice
# Reload unit files after changes
systemctl daemon-reload
Viewing Logs
# Service logs
journalctl -u myservice
# Follow logs
journalctl -u myservice -f
# Since boot
journalctl -u myservice -b
# Last N lines
journalctl -u myservice -n 100
# Time range
journalctl -u myservice --since "2024-01-01" --until "2024-01-02"
Timer Units
# /etc/systemd/system/mybackup.timer
[Unit]
Description=Run backup daily
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
# /etc/systemd/system/mybackup.service
[Unit]
Description=Backup Service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
Dependencies
[Unit]
# Start after these
After=network.target postgresql.service
# Require these (fail if they fail)
Requires=postgresql.service
# Want these (don't fail if they fail)
Wants=redis.service
# Conflict with these
Conflicts=other.service
Environment Variables
[Service]
Environment="VAR1=value1" "VAR2=value2"
EnvironmentFile=/etc/myservice/env
Resource Limits
[Service]
LimitNOFILE=65535
LimitNPROC=4096
MemoryLimit=1G
CPUQuota=50%
Debugging
# Analyze boot time
systemd-analyze
systemd-analyze blame
systemd-analyze critical-chain myservice
# List all units
systemctl list-units
systemctl list-unit-files
# Show dependencies
systemctl list-dependencies myservice
# Verify unit file
systemd-analyze verify myservice.service