Run your own application as a systemd service (Node.js, Python, Go)
Keep a program running in the background with systemd: write a unit file, start and restart automatically, environment variables, a dedicated user and logs with journalctl.
You wrote a Node.js app, a Python bot or a Go program and want it to run permanently, restart after a crash and come back up by itself after a server reboot. You need no screen, no nohup and no extra tool: systemd is already there on every modern Linux.
A dedicated user for the app
Never run applications as root. Create a system user without login:
sudo useradd --system --create-home --home-dir /opt/myapp --shell /usr/sbin/nologin myapp
Copy your application to /opt/myapp and hand it to the user:
sudo chown -R myapp:myapp /opt/myapp
The unit file
sudo nano /etc/systemd/system/myapp.service
[Unit]
Description=My App
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=myapp
Group=myapp
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/node /opt/myapp/server.js
Restart=on-failure
RestartSec=5
EnvironmentFile=/opt/myapp/.env
# some protection for the system
NoNewPrivileges=true
ProtectSystem=full
PrivateTmp=true
[Install]
WantedBy=multi-user.target
ExecStart needs the full path to the program. Find it with which node or which python3. For a Python program in a virtual environment the line is, for example, ExecStart=/opt/myapp/venv/bin/python /opt/myapp/bot.py.
Environment variables
Secrets such as tokens and passwords do not belong in the unit file but in the EnvironmentFile:
sudo nano /opt/myapp/.env
PORT=3000
DATABASE_URL=mysql://myapp:password@localhost/myapp
sudo chown myapp:myapp /opt/myapp/.env
sudo chmod 600 /opt/myapp/.env
Start it
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
systemctl status myapp
enable starts the service on every boot, --now also starts it right away.
The most important commands
| Command | Effect |
|---|---|
sudo systemctl restart myapp |
restart, for example after an update |
sudo systemctl stop myapp |
stop |
sudo systemctl disable myapp |
no longer start on boot |
journalctl -u myapp -f |
follow the output live |
journalctl -u myapp --since "1 hour ago" |
output of the last hour |
sudo systemctl daemon-reload |
after every change to the unit file |
Everything your program writes to the console ends up in the journal automatically. You need no log files of your own.
If the service does not start
systemctl status myapp
journalctl -u myapp -n 50 --no-pager
Common causes:
status=203/EXEC: The path inExecStartis wrong or the file is not executable.status=217/USER: The user does not exist.- Permission denied: The user cannot read or write files in the working directory.
- Starts and dies immediately: An error in the program itself; the journal shows the message.
Restarting too often?
If a program crashes constantly, systemd gives up after five attempts within ten seconds. That is intended. Fix the error and reset the counter:
sudo systemctl reset-failed myapp
sudo systemctl start myapp
Make it reachable from outside
If your app listens on 127.0.0.1:3000, make it reachable under your domain with HTTPS using Nginx as a reverse proxy or Caddy.
For the community
Know your stuff? Write a guide.
Share what you know: we review every submitted guide, publish it under your name and say thank you with credit for your services.