Running an app with node server.js or python app.py in an SSH session works until you close the session, the app crashes, or the server reboots. systemd — the init system on every major Linux distribution — solves all three: it starts your app at boot, restarts it if it dies, collects its logs and can limit what it is allowed to do.
This guide covers everyday systemctl and journalctl use, then shows how to turn any program into a proper service.
Everyday commands
sudo systemctl status nginx # state, PID, memory, last log lines
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx # stop + start
sudo systemctl reload nginx # re-read config without dropping connections (if supported)
sudo systemctl enable nginx # start at boot
sudo systemctl enable --now nginx # enable and start in one step
sudo systemctl disable nginx
systemctl is-active nginx # prints "active" or "inactive" (handy in scripts)
systemctl list-units --type=service --state=running
systemctl list-units --failed
In the output of status, the important parts are Loaded (is the unit enabled?) and Active (is it running, and since when?).
Reading logs with journalctl
Everything a service prints to standard output or standard error ends up in the journal:
sudo journalctl -u nginx # all logs for one service
sudo journalctl -u nginx -n 100 # last 100 lines
sudo journalctl -u nginx -f # follow live
sudo journalctl -u nginx --since "2026-11-19 09:00" --until "10:00"
sudo journalctl -p err -b # errors since the last boot
sudo journalctl -k # kernel messages (OOM kills, disk errors)
The journal can grow large. Check and cap it:
journalctl --disk-usage
sudo journalctl --vacuum-size=500M
To make the limit permanent, set SystemMaxUse=500M in /etc/systemd/journald.conf and restart systemd-journald.
Writing your own service
Suppose you have a Node.js API in /srv/api that listens on port 3000. Create a dedicated user so the app does not run as root:
sudo useradd --system --home /srv/api --shell /usr/sbin/nologin api
sudo chown -R api:api /srv/api
Then create /etc/systemd/system/api.service:
[Unit]
Description=Example API
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=api
Group=api
WorkingDirectory=/srv/api
Environment=NODE_ENV=production
EnvironmentFile=-/srv/api/.env
ExecStart=/usr/bin/node /srv/api/server.js
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Load and start it:
sudo systemctl daemon-reload
sudo systemctl enable --now api
sudo systemctl status api
Run daemon-reload every time you edit a unit file.
What the directives mean
| Directive | Purpose |
|---|---|
After= / Wants=network-online.target |
Start after the network is up |
User= / Group= |
Run as an unprivileged account |
WorkingDirectory= |
Directory the process starts in |
Environment= / EnvironmentFile= |
Settings and secrets (the - means “ignore if missing”) |
ExecStart= |
The command — use absolute paths |
Restart=on-failure |
Restart if it exits with an error or is killed |
RestartSec= |
Wait between restarts |
WantedBy=multi-user.target |
Start during normal boot when enabled |
Find the absolute path of a program with which node or which python3. If you use a version manager such as nvm or pyenv, the path is inside the user’s home directory; point ExecStart at it directly.
Examples for other languages
Python (Gunicorn with a virtual environment):
ExecStart=/srv/app/venv/bin/gunicorn --workers 3 --bind 127.0.0.1:8000 app.wsgi:application
A full walkthrough is in deploying a Python app with Gunicorn and Nginx.
Go (single binary):
ExecStart=/srv/app/myapp -config /srv/app/config.yaml
Java:
ExecStart=/usr/bin/java -Xms512m -Xmx1g -jar /srv/app/app.jar
SuccessExitStatus=143
SuccessExitStatus=143 tells systemd that Java’s normal response to a stop signal is not a failure.
Restart behaviour and crash loops
Restart=on-failure is right for most apps. Restart=always also restarts after a clean exit, which suits programs that should never stop.
If an app crashes repeatedly, systemd stops trying after 5 failures in 10 seconds by default and marks the unit as failed. Adjust in the [Unit] section:
StartLimitIntervalSec=300
StartLimitBurst=10
When a unit is in the failed state, fix the cause, then sudo systemctl reset-failed api && sudo systemctl start api.
Resource limits
Stop one app from starving the rest of the server:
[Service]
MemoryMax=1G # kill and restart if it goes above 1 GB
CPUQuota=200% # at most 2 CPU cores
TasksMax=512 # limit threads/processes
LimitNOFILE=65535 # open file limit (sockets count as files)
Check current usage with systemctl status api or systemd-cgtop.
Security hardening in a few lines
systemd can sandbox a service so that even if it is exploited, the attacker sees very little:
[Service]
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=/srv/api/uploads /srv/api/logs
ProtectKernelTunables=true
ProtectControlGroups=true
RestrictSUIDSGID=true
ProtectSystem=strict makes the whole filesystem read-only for the service except the paths in ReadWritePaths. Add these one at a time and test, as some apps need extra paths. systemd-analyze security api scores a unit and lists what else could be restricted.
Overriding packaged services
Do not edit unit files in /usr/lib/systemd/system/ — package updates overwrite them. Use an override instead:
sudo systemctl edit nginx
This opens an empty drop-in file. For example, to always restart Nginx if it crashes:
[Service]
Restart=on-failure
RestartSec=3
systemctl cat nginx shows the original unit plus your overrides.
Timers: a modern alternative to cron
systemd timers run a service on a schedule, with logging and missed-run catch-up built in. Create /etc/systemd/system/cleanup.service:
[Unit]
Description=Clean temp uploads
[Service]
Type=oneshot
User=api
ExecStart=/usr/bin/find /srv/api/uploads/tmp -type f -mtime +2 -delete
And /etc/systemd/system/cleanup.timer:
[Unit]
Description=Daily temp cleanup
[Timer]
OnCalendar=*-*-* 03:30
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now cleanup.timer
systemctl list-timers
Persistent=true runs a missed job after the server comes back up. For simple jobs, classic cron is still fine — see scheduling tasks with cron.
Troubleshooting
| Symptom | Check |
|---|---|
status=203/EXEC |
ExecStart path is wrong or the file is not executable |
status=217/USER |
The User= account does not exist |
| Works in your shell, fails as a service | Missing environment variables or relative paths; services do not load your .bashrc |
| Port already in use | `sudo ss -tulpn |
| Changes ignored | Forgot sudo systemctl daemon-reload |