06-22-2026, 12:25 PM
systemd is everywhere on modern Linux and understanding how to write service files means you can run any process reliably as a system service with auto-restart, logging, and dependency management.
A minimal service file
Save to /etc/systemd/system/myapp.service
Essential commands
Useful Service options
Type values that matter:
Restart policies:
A minimal service file
Code:
[Unit]
Description=My Application
After=network.target
[Service]
Type=simple
User=myapp
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/myapp --config /etc/myapp/config.yml
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.targetSave to /etc/systemd/system/myapp.service
Essential commands
Code:
sudo systemctl daemon-reload # reload after editing a .service file
sudo systemctl enable myapp # start on boot
sudo systemctl start myapp # start now
sudo systemctl restart myapp # restart
sudo systemctl status myapp # check status + recent logs
sudo journalctl -u myapp -f # follow live logs
sudo journalctl -u myapp --since today # logs from todayUseful Service options
Code:
Environment=NODE_ENV=production # set env vars
EnvironmentFile=/etc/myapp/.env # load from file
StandardOutput=journal # send stdout to journald
StandardError=journal
LimitNOFILE=65535 # raise file descriptor limit
PrivateTmp=yes # isolated /tmp
NoNewPrivileges=yes # security hardeningType values that matter:
- simple - process started by ExecStart IS the service
- forking - process forks and parent exits (old daemon style)
- notify - process signals systemd when ready (more reliable than simple for slow-starting services)
Restart policies:
- always - always restart, regardless of exit code
- on-failure - restart only on non-zero exit
- on-abnormal - restart on signal/timeout/watchdog, not clean exit
