Linux and servers · Intermediate · 12 min
How to Run Your App as a systemd Service on Ubuntu 26.04
By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS · systemd
You will write a systemd unit file that starts your app at boot, restarts it when it crashes, runs it as an unprivileged user, and sends its logs to the journal.
On this page
If you start your app by typing a command in an SSH session, it stops when you log out, and it does not come back after a reboot or a crash. systemd is the service manager on Ubuntu. Give it a short unit file and it starts the app at boot, restarts it on failure, controls the account it runs under, and collects its output.
In this tutorial you will create a service account, write a unit file for a small web app, start and enable the service, and read its logs. Ubuntu 26.04 ships systemd 259.
Step 1 · Create an account for the app
Do not run an app as root. If it is compromised, the attacker has the machine. Make a system user that has no password, no login shell and its own group.
sudo apt update
sudo apt install -y python3
sudo adduser --system --group --no-create-home --home /srv/myapp myapp
sudo mkdir -p /srv/myapp
echo '<h1>It runs</h1>' | sudo tee /srv/myapp/index.html
sudo chown -R myapp:myapp /srv/myapp--system creates a user with a low ID and no expiry. --group creates a group with the same name. Replace myapp and /srv/myapp with your own app's name and folder.
Check it worked
getent passwd myappThe line ends with /srv/myapp:/usr/sbin/nologin. The user cannot start a login session.
Step 2 · Run the app by hand first
Always run the command yourself before putting it in a unit. An error is far easier to read in a terminal. This app serves the folder over HTTP on the local port 8080.
sudo -u myapp /usr/bin/python3 -m http.server 8080 --bind 127.0.0.1 --directory /srv/myapp &
app_pid=$!
sleep 2
python3 -c 'import urllib.request; print(urllib.request.urlopen("http://127.0.0.1:8080/").read().decode())'
kill "$app_pid"The page prints <h1>It runs</h1>. Two things to take from this for the unit file: systemd needs the full path to the program (/usr/bin/python3, not python3), and the app must run in the foreground. A program that forks itself into the background confuses Type=simple, which is the default and the right choice for most apps.
Step 3 · Write the unit file
Unit files for your own services go in /etc/systemd/system/. The file name sets the service name.
sudo tee /etc/systemd/system/myapp.service > /dev/null <<'UNIT'
[Unit]
Description=My app (static site on port 8080)
After=network.target
[Service]
Type=simple
User=myapp
Group=myapp
WorkingDirectory=/srv/myapp
ExecStart=/usr/bin/python3 -m http.server 8080 --bind 127.0.0.1
Restart=on-failure
RestartSec=5
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
[Install]
WantedBy=multi-user.target
UNITWhat each line does:
After=network.targetstarts the app after networking is up.UserandGrouprun it as the account from step 1.WorkingDirectoryis where the process starts. It is also the folder relative paths use.ExecStartis the command. Use absolute paths. systemd does not use a shell, so&&, pipes and~do not work here.Restart=on-failurerestarts the app after a crash or non-zero exit, but not after you stop it on purpose.RestartSec=5waits five seconds between attempts.NoNewPrivileges,ProtectSystem=strict,ProtectHomeandPrivateTmplimit what the process can touch. WithProtectSystem=strictthe whole file system is read-only for the app, apart from/dev,/procand/sys. If your app must write somewhere, addReadWritePaths=/srv/myapp/data.WantedBy=multi-user.targetis whatenablehooks into so the service starts at boot.
Check it worked
Ask systemd to read the file and report problems:
systemd-analyze verify /etc/systemd/system/myapp.serviceNo output means no errors. A mistyped option is reported, for example Failed to parse Restart=sometimes, ignoring: Invalid argument. Read warnings as faults: systemd ignores the bad line and carries on, so the service would not behave as you meant.
Step 4 · Start the service and enable it at boot
systemd reads unit files at start and when told to reload. After you create or change one, reload it:
sudo systemctl daemon-reloadStart the service now, and enable it to start at every boot. enable --now does both.
sudo systemctl enable --now myappsystemd answers with a line that starts Created symlink and names the multi-user.target.wants entry for myapp.service.
Check it worked
systemctl status myappThe output includes Active: active (running), the main process ID and the last few log lines. Then request the page:
curl http://127.0.0.1:8080/You should see <h1>It runs</h1>.
Troubleshooting
When the status shows failed or the service restarts in a loop, read the reason in the journal:
journalctl -u myapp -n 50 --no-pagerTypical causes:
status=203/EXEC: systemd could not run theExecStartprogram. The path is wrong, the file is not executable, or the user cannot read it. Runls -lon the path.status=200/CHDIR: theWorkingDirectorydoes not exist or the user cannot enter it.status=217/USER: the account inUser=does not exist. Checkgetent passwd myapp.- Port already in use in the log: another process holds port 8080. Find it with
sudo ss -ltnp 'sport = :8080'. Read-only file systemerrors in the app:ProtectSystem=strictis blocking a write. Add the folder toReadWritePaths=.
Step 5 · Read logs and manage the service
Anything the app prints to standard output or standard error goes to the journal, with the service name and timestamps. No log file setup is needed.
journalctl -u myapp -f-f follows new entries, like tail -f. Press Ctrl+C to stop. Add --since "1 hour ago" to limit the range.
The commands you will use most:
| Command | What it does |
|---|---|
| sudo systemctl restart myapp | Stop and start the service |
| sudo systemctl reload myapp | Ask the app to reread its config, if the unit defines ExecReload= |
| sudo systemctl stop myapp | Stop it. With Restart=on-failure it stays stopped |
| sudo systemctl disable myapp | Do not start it at boot |
| systemctl is-enabled myapp | Print enabled or disabled |
| systemctl list-units --type=service --state=failed | List services that failed |
After editing the unit file, run sudo systemctl daemon-reload and then sudo systemctl restart myapp. Without the reload, systemd keeps using the old file and warns that it changed on disk.
To test the crash restart, kill the process and watch it return after five seconds:
sudo kill -9 $(systemctl show -p MainPID --value myapp)
sleep 6
systemctl is-active myappBecause the app was killed by a signal, systemd treats it as a failure, Restart=on-failure applies, and the last command prints active.
Next steps
- Put the app behind a web server with
/tutorials/nginx-reverse-proxy-for-a-node-app, which uses the same pattern for a Node.js app. - Run a task on a timetable instead of continuously:
/tutorials/schedule-jobs-with-cron. - Check the file ownership your service needs in
/tutorials/linux-file-permissions-chmod-chown. - Sources:
systemd.service(5),systemd.exec(5)andsystemctl(1)at freedesktop.org/software/systemd/man.
Related tutorials
- How to Schedule Jobs with Cron on LinuxYou will schedule a command to run every minute, then on a real timetable, read the cron syntax, and learn why a job that works in your shell can fail under cron.
- How to Log In to a Linux Server with SSH KeysYou will create an Ed25519 SSH key, install its public half on a server, log in without a password, and save the connection as a short name.
- How File Permissions Work in Linux: chmod, chown and umaskYou will read Linux permission strings, change them with chmod and chown, set up a folder two users can share, and control the defaults with umask.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials