Updated: 28 September 2026 · Applies to: systemd 252 to 259 on AlmaLinux and Rocky Linux 9 and 10, RHEL 9 and 10, Ubuntu 24.04 and 26.04, Debian 13

Turning your own script or program into a systemd service means it starts at boot, restarts when it crashes, runs as a chosen user, and logs to the journal. All you need is a small unit file. We validated the example below with systemd-analyze verify on AlmaLinux 9.

1. Prepare the script

install -m 755 myapp.sh /usr/local/bin/myapp.sh
useradd -r -s /usr/sbin/nologin appuser

The script should run in the foreground (not start itself in the background) and print its messages to standard output, which systemd sends to the journal. Running it as its own unprivileged user limits the damage if it misbehaves.

2. Create the unit file

Create /etc/systemd/system/myapp.service:

[Unit]
Description=My application
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=appuser
ExecStart=/usr/local/bin/myapp.sh
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
  • After and Wants on network-online.target: start once the network is up, for programs that connect to other servers.
  • Type=simple: the program keeps running. For a script that does a job and exits, use Type=oneshot (and add RemainAfterExit=yes if it should count as "active" afterwards).
  • Restart=on-failure: start it again after a crash, waiting RestartSec seconds.

3. Check, enable and start

systemd-analyze verify /etc/systemd/system/myapp.service
systemctl daemon-reload
systemctl enable --now myapp
systemctl status myapp

4. Logs

journalctl -u myapp -f

Useful extra settings

  • WorkingDirectory=/opt/myapp: the folder the program runs in.
  • Environment=PORT=8080 or EnvironmentFile=/etc/myapp.env: settings for the program.
  • ProtectSystem=full and PrivateTmp=yes: simple hardening that most programs tolerate.

After every change to the unit file, run systemctl daemon-reload and restart the service. To change a unit that a package installed, use systemctl edit NAME instead of editing its file.

For a script that should only run once at boot, see how to run a script automatically at boot.

Official documentation: systemd.service and systemd.exec.

Ucartz services for this topic

Was this answer helpful? 0 Users Found This Useful (0 Votes)