Short answer
Write a unit file in /etc/systemd/system/myapp.service with User=, ExecStart= (the full path to the program) and Restart=on-failure, plus WantedBy=multi-user.target. Then run sudo systemctl daemon-reload and sudo systemctl enable --now myapp. It starts now and at every boot; read its output with journalctl -u myapp.
Tested on Ubuntu 26.04 and 24.04, two separate runs. On 24.04, every step is the same. Everything that differed
Before you start
- A sudo user on Ubuntu 26.04 or 24.04.
- The program you want to run. This guide uses a small Python web app so every step can be shown with real output; your own program (a Node or Go app, a script, a jar) goes in
ExecStart=the same way.
Step 1: Put the program in place
$ sudo mkdir -p /opt/hello$ sudo tee /opt/hello/app.py > /dev/null <<'EOF'import http.server, os PORT = int(os.environ.get('PORT', '8000')) class Hello(http.server.BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.end_headers() self.wfile.write(b'hello from a systemd service\n') print(f'listening on 127.0.0.1:{PORT}', flush=True)http.server.HTTPServer(('127.0.0.1', PORT), Hello).serve_forever()EOFIt prints one line when it starts and answers web requests on port 8000 (or the port in PORT). systemd needs the full path to the program, so look it up:
$ command -v python3 python/usr/bin/python3Only python3 exists. Ubuntu has no python command, and a unit that says /usr/bin/python fails with 203/EXEC (see below).
Step 2: Give it its own user
Without User=, a service runs as root. A user of its own, with no home and no login shell, means a bug in the program can only touch what that user may:
$ sudo useradd --system --no-create-home --shell /usr/sbin/nologin hello$ id hellouid=999(hello) gid=986(hello) groups=986(hello)A system user (number below 1000) with its own group.
Step 3: Write the unit file
$ sudo tee /etc/systemd/system/hello.service <<'EOF'[Unit]Description=Hello web appAfter=network.target [Service]User=helloGroup=helloWorkingDirectory=/opt/helloEnvironment=PORT=8000ExecStart=/usr/bin/python3 /opt/hello/app.pyRestart=on-failureRestartSec=2 [Install]WantedBy=multi-user.targetEOFAfter=network.target: start after the network is set up.User=andGroup=: who it runs as.WorkingDirectory=: its current folder, so relative paths in your program work.Environment=: settings your program reads. For many settings or secrets, useEnvironmentFile=/etc/hello.envinstead.ExecStart=: the full path to the program and its arguments. No shell runs it, socd,&&and>do not work here.Restart=on-failureandRestartSec=2: if it crashes, start it again after 2 seconds.WantedBy=multi-user.target: whatenablehooks it to, so it starts at boot.
Check the file before you use it:
$ sudo systemd-analyze verify /etc/systemd/system/hello.service/usr/lib/systemd/system/xfs_scrub_all.service:26: Support for option CPUAccounting= has been removed and it is ignored/usr/lib/systemd/system/system-xfs_scrub.slice:15: Support for option CPUAccounting= has been removed and it is ignoredNothing about hello.service means the file is fine. On Ubuntu 26.04 it also printed two warnings about xfs_scrub units that Ubuntu itself ships (an option newer systemd ignores); they are not about your service. On 24.04 it printed nothing.
Step 4: Start it now and at every boot
$ sudo systemctl daemon-reload$ sudo systemctl enable --now helloCreated symlink '/etc/systemd/system/multi-user.target.wants/hello.service' → '/etc/systemd/system/hello.service'.daemon-reload makes systemd read the new file. enable creates that link so it starts at boot; --now also starts it now. start alone would not survive a reboot.
$ systemctl status hello --no-pager● hello.service - Hello web app Loaded: loaded (/etc/systemd/system/hello.service; enabled; preset: enabled) Active: active (running) since Thu 2026-11-19 22:48:47 UTC; 2s ago Invocation: 4ce30f47b79144998de20fd45bddd385 Main PID: 2863 (python3) Tasks: 1 (limit: 1486) Memory: 9.1M (peak: 9.1M) CPU: 46ms CGroup: /system.slice/hello.service └─2863 /usr/bin/python3 /opt/hello/app.pyenabled (starts at boot) and active (running), with the program’s process under it. It answers, and it runs as hello, not root:
$ curl -s http://127.0.0.1:8000/hello from a systemd service$ ps -o user=,cmd= -C python3hello /usr/bin/python3 /opt/hello/app.pyStep 5: Read its output
Everything the program prints goes to the systemd journal, with no log file to set up:
$ sudo journalctl -u hello --no-pager -n 5Nov 19 22:48:47 web-server systemd[1]: Started hello.service - Hello web app.Nov 19 22:48:47 web-server python3[2863]: listening on 127.0.0.1:8000Nov 19 22:48:50 web-server python3[2863]: 127.0.0.1 - - [19/Nov/2026 22:48:50] "GET / HTTP/1.1" 200 -systemd’s own line, the program’s print, and the request from the curl above. Add -f to follow new lines as they come.
Step 6: See it restart after a crash
Kill the program the hard way, as a crash would:
$ sudo systemctl kill -s KILL hello$ sudo journalctl -u hello --no-pager -n 6Nov 19 22:48:52 web-server systemd[1]: hello.service: Killed unit cgroup '/system.slice/hello.service' with SIGKILL on client request.Nov 19 22:48:52 web-server systemd[1]: hello.service: Main process exited, code=killed, status=9/KILLNov 19 22:48:52 web-server systemd[1]: hello.service: Failed with result 'signal'.Nov 19 22:48:54 web-server systemd[1]: hello.service: Scheduled restart job, restart counter is at 1.Nov 19 22:48:54 web-server systemd[1]: Started hello.service - Hello web app.Nov 19 22:48:54 web-server python3[3373]: listening on 127.0.0.1:8000The process died (status=9/KILL), and 2 seconds later systemd started a new one: Scheduled restart job, restart counter is at 1. A clean exit (code 0) is not a failure, so on-failure leaves it stopped; use Restart=always for a program that should run even after it exits cleanly.
$ systemctl is-active helloactiveChange the unit file: “changed on disk”
Edit the file, here to move the app to port 8001, and ask for the status before anything else:
$ sudo sed -i 's/PORT=8000/PORT=8001/' /etc/systemd/system/hello.service$ systemctl status hello --no-pager | grep -i warningWarning: The unit file, source configuration file or drop-ins of hello.service changed on disk. Run 'systemctl daemon-reload' to reload units.systemd still runs the old version: a change takes effect only after daemon-reload, and the running program keeps its old settings until a restart. Do both:
$ sudo systemctl daemon-reload && sudo systemctl restart hello$ curl -s http://127.0.0.1:8001/hello from a systemd serviceIf it fails with “status=203/EXEC”
A second unit with the most common mistake: python instead of python3 in ExecStart=.
$ sudo sed -n '/ExecStart/p' /etc/systemd/system/broken.serviceExecStart=/usr/bin/python /opt/hello/app.py$ sudo systemctl start brokensystemctl start printed nothing, as if it worked. The status tells the truth:
$ systemctl status broken --no-pager× broken.service Loaded: loaded (/etc/systemd/system/broken.service; static) Active: failed (Result: exit-code) since Thu 2026-11-19 22:49:03 UTC; 2s ago Duration: 12ms Invocation: f3db49ccf2634c21887bfed451ba980e Process: 4401 ExecStart=/usr/bin/python /opt/hello/app.py (code=exited, status=203/EXEC) Main PID: 4401 (code=exited, status=203/EXEC) Mem peak: 2M CPU: 5ms203/EXEC is systemd’s code for “could not run the program in ExecStart=”: the path is wrong, the file is not executable, or a script’s #! line names a missing interpreter. Check the path itself:
$ ls -l /usr/bin/pythonls: cannot access '/usr/bin/python': No such file or directoryFix the path, then daemon-reload and start again. The journal sometimes adds a line naming the missing file (on 26.04 it did here), but not always, so ls -l on the path is the sure test:
$ sudo journalctl -u broken --no-pager -n 4Nov 19 22:49:03 web-server (python)[4401]: broken.service: Unable to locate executable '/usr/bin/python': No such file or directoryNov 19 22:49:03 web-server (python)[4401]: broken.service: Failed at step EXEC spawning /usr/bin/python: No such file or directoryNov 19 22:49:03 web-server systemd[1]: broken.service: Main process exited, code=exited, status=203/EXECNov 19 22:49:03 web-server systemd[1]: broken.service: Failed with result 'exit-code'.If it fails with “status=217/USER”
The same unit, now with the user name mistyped:
$ sudo sed -n '/User=/p' /etc/systemd/system/broken.serviceUser=helo$ sudo systemctl start broken$ systemctl status broken --no-pager× broken.service Loaded: loaded (/etc/systemd/system/broken.service; static) Active: failed (Result: exit-code) since Thu 2026-11-19 22:49:08 UTC; 2s ago Duration: 6ms Invocation: 225c2efdd5cb4faea14fc6f404fef177 Process: 4963 ExecStart=/usr/bin/python3 /opt/hello/app.py (code=exited, status=217/USER) Main PID: 4963 (code=exited, status=217/USER) Mem peak: 1.7M CPU: 4ms$ id heloid: 'helo': no such user217/USER: the user in User= does not exist, so systemd could not switch to it. Create the user (step 2) or fix the name.
Step 7: Lock it down
systemd can rate how much of the system a service can reach:
$ systemd-analyze security hello --no-pager | tail -n 1→ Overall exposure level for hello.service: 9.2 UNSAFE 😨Add settings in a drop-in file instead of editing the unit: a drop-in survives if the main file is replaced, and shows clearly what you added. sudo systemctl edit hello opens an editor on the same file; here it is written directly:
$ sudo mkdir -p /etc/systemd/system/hello.service.d$ sudo tee /etc/systemd/system/hello.service.d/hardening.conf <<'EOF'[Service]NoNewPrivileges=yesProtectSystem=strictProtectHome=yesPrivateTmp=yesPrivateDevices=yesProtectKernelTunables=yesProtectKernelModules=yesProtectControlGroups=yesRestrictSUIDSGID=yesCapabilityBoundingSet=EOFThe whole system is read-only to it (ProtectSystem=strict), home folders are hidden, it gets its own /tmp and no devices, cannot gain privileges, and has no special capabilities. A program that must write somewhere needs that folder in ReadWritePaths=.
$ sudo systemctl daemon-reload && sudo systemctl restart hello$ systemd-analyze security hello --no-pager | tail -n 1→ Overall exposure level for hello.service: 4.8 OK 🙂$ curl -s http://127.0.0.1:8001/hello from a systemd serviceFrom 9.2 UNSAFE to 4.8 OK, and the app still answers. systemctl cat shows every file systemd reads for the service:
$ systemctl cat hello --no-pager | grep -E "^# /"# /etc/systemd/system/hello.service# /etc/systemd/system/hello.service.d/hardening.confStep 8: Check it comes back after a reboot
$ sudo reboot$ systemctl is-active hello && systemctl is-enabled helloactiveenabled$ curl -s http://127.0.0.1:8001/hello from a systemd serviceRemove the service
$ sudo systemctl disable --now helloRemoved '/etc/systemd/system/multi-user.target.wants/hello.service'.$ sudo rm -r /etc/systemd/system/hello.service /etc/systemd/system/hello.service.d && sudo systemctl daemon-reload$ systemctl status hello --no-pagerUnit hello.service could not be found.$ sudo userdel hello && sudo rm -r /opt/helloThe configuration files, ready to copy
These are the two files as they were on the test server after the reboot, with the service running (port 8001 from the change above). Change the user, paths and program for your own service.
[Unit]Description=Hello web appAfter=network.target [Service]User=helloGroup=helloWorkingDirectory=/opt/helloEnvironment=PORT=8001ExecStart=/usr/bin/python3 /opt/hello/app.pyRestart=on-failureRestartSec=2 [Install]WantedBy=multi-user.targetRead back from the test server (Ubuntu 26.04) on 2026-11-19, after the steps had worked. Placeholders such as [password]: type your own value in the box above the file to fill them in, or replace them yourself.
[Service]NoNewPrivileges=yesProtectSystem=strictProtectHome=yesPrivateTmp=yesPrivateDevices=yesProtectKernelTunables=yesProtectKernelModules=yesProtectControlGroups=yesRestrictSUIDSGID=yesCapabilityBoundingSet=Read back from the test server (Ubuntu 26.04) on 2026-11-19, after the steps had worked. Placeholders such as [password]: type your own value in the box above the file to fill them in, or replace them yourself.
Troubleshooting
status=203/EXEC: the program inExecStart=cannot be run (wrong path,pythoninstead ofpython3, not executable). Details.status=217/USER: the user inUser=does not exist. Details.- “changed on disk. Run ‘systemctl daemon-reload’”: you edited the file; reload, then restart. Details.
- It does not start after a reboot: it was started but never enabled. Run
sudo systemctl enable hello. Details. - It stays stopped after it exits:
Restart=on-failuredoes not restart a clean exit; useRestart=always. Details.
What these steps changed on the server
Both runs recorded the server before the first step and after the reboot (see the Tested on box below): the unit file and its drop-in, the boot link, the hello user, the app in /opt/hello, and a program listening on 127.0.0.1:8001. No package was added.
Ubuntu 26.04 vs 24.04
The same check ran on a fresh 26.04 server and on a fresh 24.04 server, each with its own run log. Every step worked the same; these outputs differed:
| What | Ubuntu 26.04 | Ubuntu 24.04 |
|---|---|---|
| System | Ubuntu 26.04.1 LTS7.0.0-43-generic | Ubuntu 24.04.5 LTS6.8.0-147-generic |
| systemd | 259.9-0ubuntu0.1 | 255.4-1ubuntu8.18 |
| python3 | 3.14.3-0ubuntu2 | 3.12.3-0ubuntu2.1 |
systemd-analyze verify on a good unit | two warnings about Ubuntu’s own xfs_scrub units (CPUAccounting= removed) | nothing |
| The journal line for a missing user (217/USER) | Failed to determine credentials for user 'helo': Unknown user | Failed to determine user credentials: No such process |
systemctl status | an extra Invocation: line (an id for each start) | no Invocation: line |
| Proof | Run log, 36 checks passed | Run log, 36 checks passed |
