Developer and Text Tools

How to Run Any Program as a Reliable systemd Service

A guide to systemd service units: types, restart policies, users, environment, sandboxing, timers that replace cron, and the commands to install them.

Saizul Amin
Saizul Amin2 Nov 2026 · 3 min read
How to Run Any Program as a Reliable systemd Service

How to Run Any Program as a Reliable systemd Service

Starting a program with nohup in an SSH session works until the server reboots or the program crashes at 3 a.m. On almost every modern Linux distribution, systemd is the tool that keeps programs running, restarts them on failure and starts them at boot. You describe the program in a small unit file. This guide explains what goes into one, and the free systemd unit file generator writes it with explanations.

Anatomy of a service unit

A unit file has sections in square brackets:

  • [Unit] describes the service and its relation to other units: Description, After and Wants.
  • [Service] says how to run it: Type, ExecStart, User, WorkingDirectory, Environment and Restart.
  • [Install] says when it should start once enabled, usually WantedBy=multi-user.target.

Service files for the whole system live in /etc/systemd/system; services for one user live in ~/.config/systemd/user.

Choosing the Type

  • simple or exec: the program stays in the foreground. Most modern servers, Node.js apps and Go binaries fit here.
  • notify: the program tells systemd when it is ready, as Gunicorn and many daemons can.
  • forking: classic daemons that fork into the background; give systemd a PIDFile.
  • oneshot: scripts that run and exit, such as backups.

Paths, users and environment

ExecStart needs an absolute path, such as /usr/bin/node, because systemd does not search your shell's PATH. Run services as a dedicated user instead of root. Put configuration in Environment lines or an EnvironmentFile, which keeps secrets out of the unit itself.

Restart behaviour

Restart=on-failure restarts a crashed program but not one that exited cleanly. Add RestartSec=5s to avoid hammering a failing dependency, and StartLimitBurst with StartLimitIntervalSec to stop endless restart loops.

Ordering is not the same as dependency

After=network.target only orders startup; it does not wait for a working network connection. Services that connect to databases or remote APIs at start should use After=network-online.target together with Wants=network-online.target.

Sandboxing for free

systemd can isolate a service with a few lines. NoNewPrivileges stops privilege escalation, ProtectSystem=strict makes the file system read-only except paths you allow with ReadWritePaths or StateDirectory, PrivateTmp gives the service its own /tmp, and SystemCallFilter=@system-service blocks unusual kernel calls. On the server, systemd-analyze security name.service rates the exposure from 0 to 10. Add options one at a time and restart the service after each, because some programs need specific freedoms; Java and Node.js, for example, break with MemoryDenyWriteExecute.

Timers instead of cron

A timer unit activates a service on a schedule. OnCalendar uses a readable syntax: daily, Mon..Fri 09:00 or --01 03:30:00 for the first day of each month. Persistent=true runs a missed job after the machine was off, and RandomizedDelaySec spreads jobs from many servers. Unlike cron, every run is logged in the journal with its exit status. Check an expression on the server with systemd-analyze calendar.

Socket activation

A socket unit listens on a port and starts the service only when the first connection arrives. It is useful for rarely used services and for restarting a service without dropping incoming connections.

Installing and checking

After copying the files, run systemctl daemon-reload, then systemctl enable --now name.service. systemctl status shows whether it runs, and journalctl -u name -f follows its logs. For timers, systemctl list-timers shows the next run.

Limits

A unit file only describes how to run a program; it cannot fix a program that leaks memory or needs a different working directory than you think. Distribution versions of systemd differ, and a few options in this guide require recent releases. Test changes on a non-production machine first.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages