On this page
- Why use systemd instead of screen or nohup?
- How do I create a user for the service?
- What goes into the unit file?
- How do I start, enable and check the service?
- How do restart options behave?
- Which hardening options should I add?
- How do I read the service logs?
- How do I change a unit without editing the original file?
- How do I troubleshoot a service that will not start?
- Questions
Why use systemd instead of screen or nohup?
systemd supervises the process for you: it starts the app at boot, brings it back after a crash, stops it cleanly on shutdown and keeps its logs. A program left in a screen session or started with nohup dies with the next reboot and nobody notices. Every image we offer for Ubuntu, Debian, Rocky Linux and AlmaLinux boots with systemd, so the same unit file works on all of them.
This guide uses a small web app in /opt/myapp listening on port 8080 as the example. It could be a Node.js server, a Python API, a Go binary or a trading bot; only the ExecStart line changes. If you only need a terminal that survives disconnects while you experiment, tmux is the better tool.
How do I create a user for the service?
Run each app under its own system account with no login shell, so a bug in the app cannot touch the rest of the server:
sudo useradd --system --home-dir /opt/myapp --shell /usr/sbin/nologin myapp
sudo mkdir -p /opt/myapp /var/lib/myapp
sudo chown -R myapp:myapp /opt/myapp /var/lib/myapp
id myappOn Rocky and Alma the shell path /sbin/nologin works as well. Copy your code or binary into /opt/myapp and run it once by hand as that user to rule out path or permission errors before systemd is involved:
sudo -u myapp /opt/myapp/bin/server --port 8080What goes into the unit file?
A service unit has three sections: [Unit] for description and ordering, [Service] for how to run the process and [Install] for when to start it. Create the file with sudo nano /etc/systemd/system/myapp.service:
[Unit]
Description=My web app
After=network-online.target
Wants=network-online.target
StartLimitIntervalSec=300
StartLimitBurst=5
[Service]
Type=exec
User=myapp
Group=myapp
WorkingDirectory=/opt/myapp
EnvironmentFile=-/etc/myapp/env
ExecStart=/opt/myapp/bin/server --port 8080
Restart=on-failure
RestartSec=5
TimeoutStopSec=20
[Install]
WantedBy=multi-user.targetExecStart needs an absolute path and is not run through a shell, so pipes and && do not work there; wrap complex commands in a script. The dash in front of the EnvironmentFile path makes the file optional. Type=exec reports failure if the binary cannot be started at all; use Type=notify only when the program speaks the sd_notify protocol.
How do I start, enable and check the service?
Reload systemd so it reads the new file, then enable and start in one step:
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
systemctl status myapp
systemctl is-enabled myapp
ss -tlnp | grep 8080
curl -I http://127.0.0.1:8080The status output shows active (running), the main PID, memory use and the last log lines. Run daemon-reload every time you edit the unit file; systemd warns in the status output when the file on disk differs from the loaded copy. Use sudo systemctl restart myapp to apply code changes and reload only when the app supports a reload signal through ExecReload=.
How do restart options behave?
Restart=on-failure brings the app back after a crash, a non-zero exit code or a kill signal, but not after a clean exit or a systemctl stop. Use Restart=always for programs that may exit with code 0 and should still come back. RestartSec=5 waits five seconds between attempts so a failing app does not spin the CPU.
The start limit in [Unit] stops the loop: with the values above, five failed starts in 300 seconds put the unit into a failed state. After fixing the cause, clear it:
sudo systemctl reset-failed myapp
sudo systemctl start myappTo test the policy, kill the process and watch systemd revive it: sudo systemctl kill -s SIGKILL myapp, wait a few seconds, then check systemctl status myapp.
Which hardening options should I add?
Sandbox options limit what the process can see and change, at almost no cost. Add these to the [Service] section and adjust paths for your app:
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ProtectHome=yes
ReadWritePaths=/var/lib/myapp
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
RestrictSUIDSGID=yes
LockPersonality=yes
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
MemoryMax=512MProtectSystem=strict makes the whole file system read only except the paths you list in ReadWritePaths. MemoryMax caps RAM so a leak kills only this service instead of waking the OOM killer for the whole box. If the app must bind a port below 1024 without root, add AmbientCapabilities=CAP_NET_BIND_SERVICE. Rate the result with:
systemd-analyze security myappLower scores are better. Apply options one at a time and restart after each, because an over-strict sandbox shows up as Permission denied or Read-only file system in the log.
How do I read the service logs?
Anything the app writes to standard output or standard error lands in the journal under the unit name:
journalctl -u myapp -f
journalctl -u myapp --since "1 hour ago"
journalctl -u myapp -b -p warning
journalctl -u myapp -o cat -n 100-b limits output to the current boot, -p warning filters by priority and -o cat hides the timestamp prefix. There is no need for the app to write its own log file. The journalctl guide covers retention and filters in depth.
How do I change a unit without editing the original file?
Use a drop-in override; it survives package updates that replace the vendor unit. This is the right way to tweak services installed by apt or dnf, such as Nginx or PostgreSQL:
sudo systemctl edit nginxThe editor opens /etc/systemd/system/nginx.service.d/override.conf. Add only the lines you want to change, for example a higher open file limit:
[Service]
LimitNOFILE=65535systemd reloads automatically when you save from systemctl edit. To clear a list option such as ExecStart before setting a new one, write an empty ExecStart= line first. View the merged result with systemctl cat nginx.
How do I troubleshoot a service that will not start?
Read the last log lines and the exit status first; they almost always name the problem:
systemctl status myapp --no-pager -l
journalctl -u myapp -n 50 --no-pager
systemd-analyze verify /etc/systemd/system/myapp.servicestatus=203/EXEC means the binary path is wrong or not executable, status=200/CHDIR points to a missing WorkingDirectory, and status=217/USER means the account does not exist. On Rocky and Alma, SELinux can also block a binary placed in an unusual directory; check sudo ausearch -m avc -ts recent and read SELinux basics. To remove a service completely, run sudo systemctl disable --now myapp, delete the unit file and run daemon-reload.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Run an app as a systemd service
Where should I put my unit files?
Your own units belong in /etc/systemd/system/. Files in /usr/lib/systemd/system/ or /lib/systemd/system/ come from packages and get overwritten on updates.
Can a normal user run services without sudo?
Yes, with user units in ~/.config/systemd/user/ managed by systemctl --user. Run sudo loginctl enable-linger deploy so they keep running after the user logs out and start at boot.
How do I pass secrets to the service?
Put them in an EnvironmentFile owned by root with mode 600, or use LoadCredential=. The environment variables guide compares both.
What is the difference between enable and start?
start runs the service now; enable links it into the boot target so it starts on the next boot. enable --now does both.
Should I use Restart=always?
Use it when the program might exit cleanly but should still run all the time. For most servers on-failure is enough and lets you stop the app on purpose without a fight.
More in Server basics
- Create a sudo user
- Add a swap file
- Change the hostname
- Time zone and time sync
- File and directory permissions
- Choose a Linux distribution
- Upgrade Ubuntu to the next LTS
- Upgrade Debian to a new major release
- Rocky or AlmaLinux to a new major version
- Automatic security updates
- Schedule jobs with cron and systemd timers
- Environment variables on Linux
- Check disk space and find what fills it
- Inspect processes and memory
- Read the hardware and system report
- Reboot and shut down safely
- Customise the login message (MOTD)
- Remote desktop with Xfce and xrdp
- SELinux on Rocky Linux and AlmaLinux
- Keep sessions alive with tmux
- SFTP-only users in a chroot
- apt and dnf package management
- First-hour checklist for a new server
- Upgrade FreeBSD to a new release