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
AfterandWantsonnetwork-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, useType=oneshot(and addRemainAfterExit=yesif it should count as "active" afterwards).Restart=on-failure: start it again after a crash, waitingRestartSecseconds.
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=8080orEnvironmentFile=/etc/myapp.env: settings for the program.ProtectSystem=fullandPrivateTmp=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
- KVM VPS hosting: NVMe SSD and full root access, with Free Basic Managed Support.
- Unmanaged dedicated servers: full root access and free IPMI KVM, with Free Basic Managed Support.
- Linux server management: hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.
