systemd Service Files

Creating and managing systemd service files.

Basic Service File

/etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/var/www/myapp
ExecStart=/usr/bin/node server.js
Restart=on-failure

[Install]
WantedBy=multi-user.target

Service Types

simple
Process runs in foreground

forking
Process forks and parent exits

oneshot
Runs once then exits

notify
Service sends notification when ready

Restart Policies

Restart options
no // never restart
always // always restart
on-failure // restart on error
on-abnormal // restart on crash

Restart delay
RestartSec=5 // wait 5 seconds

Reload Configuration

After editing service file
sudo systemctl daemon-reload

Restart service
sudo systemctl restart myapp

Enable and start
sudo systemctl enable --now myapp