Systemd service management, unit files, timers, and journal logging on Linux. Use when user mentions "systemd", "systemctl", "service file", "unit file", "journalctl", "system service", "daemon", "auto-start on boot", "timer", "systemd timer", "service restart", "enable service", "journald", "system logs", or managing Linux services and daemons.
Systemd service management, unit files, timers, and journal logging on Linux. Use when user mentions "systemd", "systemctl", "service file", "unit file", "journalctl", "system service", "daemon", "auto-start on boot", "timer", "systemd timer", "service restart", "enable service", "journald", "system logs", or managing Linux services and daemons.
Systemd
systemctl Commands
sudo systemctl start myapp
sudo systemctl stop myapp
sudo systemctl restart myapp
sudo systemctl reload myapp # Reload config without full restartsudo systemctl enable myapp # Auto-start on bootsudo systemctl enable --now myapp # Enable and start immediatelysudo systemctl disable myapp
systemctl status myapp
systemctl is-active myapp
systemctl is-enabled myapp
systemctl show myapp -p MainPID,MemoryCurrent
sudo systemctl daemon-reload # Reload unit files after editing
systemctl list-units --type=service --state=failed
systemctl list-unit-files --type=service
systemctl list-timers
Writing Unit Files
Place system services in /etc/systemd/system/myapp.service.
[Service]Environment=NODE_ENV=production
Environment=PORT=3000 HOST=0.0.0.0EnvironmentFile=/etc/myapp/env
EnvironmentFile=-/etc/myapp/env.local # Dash prefix: no error if missing
Environment file (/etc/myapp/env): one VAR=VALUE per line.
Restart Policies
[Service]Restart=on-failure
RestartSec=5StartLimitIntervalSec=300# Rate-limit windowStartLimitBurst=5# Max restarts within window
Policy
Clean exit
Unclean signal
Timeout
Watchdog
no
-
-
-
-
on-failure
-
Yes
Yes
Yes
on-abnormal
-
Yes
Yes
Yes
always
Yes
Yes
Yes
Yes
Reset a failed unit: sudo systemctl reset-failed myapp
Resource Limits
[Service]MemoryMax=512M
MemoryHigh=400M # Throttle before hard limitCPUQuota=200% # 2 full cores maxCPUWeight=50# Relative weight (default 100)LimitNOFILE=65536LimitNPROC=4096
Dependency Management
[Unit]# Ordering only (does NOT pull in the dependency)After=network.target redis.service
Before=nginx.service
# Pull in dependenciesRequires=postgresql.service # Hard: if postgres fails, this fails tooWants=redis.service # Soft: continues even if redis failsBindsTo=docker.service # Like Requires + stop when docker stops# For services needing actual network connectivityAfter=network-online.target
Wants=network-online.target
User Services
Place units in ~/.config/systemd/user/. No root required.
# ~/.config/systemd/user/myapp.service[Unit]Description=My User Application
[Service]Type=simple
ExecStart=%h/bin/myapp
Restart=on-failure
Environment=PORT=8080[Install]WantedBy=default.target
systemctl --user daemon-reload
systemctl --user enable --now myapp
journalctl --user -u myapp -f
sudo loginctl enable-linger $USER# Run without active login session
%h expands to the user's home directory.
Timers (Cron Replacement)
# /etc/systemd/system/backup.timer[Unit]Description=Daily backup timer
[Timer]OnCalendar=*-*-* 02:00:00Persistent=true# Run missed executions after downtimeRandomizedDelaySec=900[Install]WantedBy=timers.target
# /etc/systemd/system/deploy-watcher.path[Unit]Description=Watch for deployment triggers
[Path]PathChanged=/var/deploy/trigger # Trigger on write + closePathExists=/var/deploy/run-now # Trigger when file appearsPathModified=/var/deploy/config # Trigger on any writeUnit=deploy.service
[Install]WantedBy=multi-user.target