TalkativeTurtles
systemd services - writing and managing your own .service files - Printable Version

+- TalkativeTurtles (https://talkativeturtles.club)
+-- Forum: Technology (https://talkativeturtles.club/forumdisplay.php?fid=2)
+--- Forum: Operating Systems & Linux (https://talkativeturtles.club/forumdisplay.php?fid=13)
+--- Thread: systemd services - writing and managing your own .service files (/showthread.php?tid=103)



systemd services - writing and managing your own .service files - Zero Two - 06-22-2026

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
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.target

Save 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 today

Useful 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 hardening

Type 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