A Discord or Telegram bot, an API or a small web app should keep running when you close SSH, restart by itself after a crash and start again after a reboot. Linux has a built-in tool for exactly this: systemd. You don't need screen, tmux or extra software.
The example runs a Python bot from /opt/mybot; the Node.js differences are shown along the way. The commands are for Ubuntu and Debian and use sudo.
1. Create a user and a folder for the app
Running the app as its own user instead of root limits the damage if the app is ever hacked:
sudo adduser --system --group --home /opt/mybot mybot Copy your code into the folder (here from ~/mybot, for example after a git clone) and give it to the new user:
sudo cp -r ~/mybot/. /opt/mybot/
sudo chown -R mybot:mybot /opt/mybot 2. Install the dependencies
Python: Ubuntu 24.04 and Debian 12 don't allow pip install into the system Python (you get an externally-managed-environment error). Use a virtual environment inside the app folder:
sudo apt install -y python3-venv
sudo -u mybot python3 -m venv /opt/mybot/venv
sudo -u mybot /opt/mybot/venv/bin/pip install -r /opt/mybot/requirements.txt Node.js: install Node.js for the whole system (from your distribution or NodeSource), not with nvm, because nvm installs it in your own home folder where the service user can't reach it. Then install the packages:
cd /opt/mybot
sudo -u mybot npm ci --omit=dev npm ci needs a package-lock.json; without one, use npm install --omit=dev.
3. Put secrets in an environment file
Keep tokens and passwords out of your code. Create /opt/mybot/.env with one NAME=value per line:
BOT_TOKEN=paste-your-token-here Then make it readable only by the app user:
sudo chown mybot:mybot /opt/mybot/.env
sudo chmod 600 /opt/mybot/.env 4. Test the start command by hand
cd /opt/mybot
sudo -u mybot /opt/mybot/venv/bin/python bot.py For Node.js: sudo -u mybot node index.js. If the app starts, stop it with Ctrl+C. If it only complains that a setting such as BOT_TOKEN is missing, that's fine: systemd loads the .env file once the service is set up in step 5. Note the full path of the program; for Node.js, which node shows it (usually /usr/bin/node).
5. Create the service
Create the file with sudo nano /etc/systemd/system/mybot.service:
[Unit]
Description=My bot
After=network-online.target
Wants=network-online.target
[Service]
User=mybot
Group=mybot
WorkingDirectory=/opt/mybot
ExecStart=/opt/mybot/venv/bin/python /opt/mybot/bot.py
EnvironmentFile=/opt/mybot/.env
Environment=PYTHONUNBUFFERED=1
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target ExecStartis the command from step 4 with full paths. For Node.js:ExecStart=/usr/bin/node /opt/mybot/index.js.EnvironmentFileloads your.env, so the app can readBOT_TOKENlike any environment variable. Remove the line if you don't use one.PYTHONUNBUFFERED=1makes Python write its output to the log right away instead of in batches. It does nothing for other languages.Restart=alwaysstarts the app again 5 seconds after it stops or crashes.
6. Start it and turn on autostart
sudo systemctl daemon-reload
sudo systemctl enable --now mybot
systemctl status mybot The status should say active (running). Press q to leave the status view. The app now starts by itself after every reboot.
7. Read the logs
Everything your app prints ends up in the system journal:
sudo journalctl -u mybot -f # follow live, Ctrl+C to stop
sudo journalctl -u mybot --since today # everything from today
sudo journalctl -u mybot -n 50 # last 50 lines 8. Update your app
Copy or pull the new code into /opt/mybot, install new dependencies if needed (step 2), fix the owner and restart:
sudo chown -R mybot:mybot /opt/mybot
sudo systemctl restart mybot If you change the .service file itself, run sudo systemctl daemon-reload before the restart. To stop the app for good, use sudo systemctl disable --now mybot.
Something not working?
status=203/EXECin the status: the program path inExecStartis wrong. Check it withls -l.Permission deniedin the log: some files don't belong to the app user. Run thechowncommand from step 8 again.- The app restarts every few seconds: it crashes on start. The last lines of
journalctl -u mybot -n 50show why. - The app can't find its files: it uses relative paths, so check that
WorkingDirectoryis the app folder.
Prefer containers? Docker can keep apps running too, with restart: unless-stopped. See Install Docker on your VPS.
Last updated 2026-11-08. Need help? Open a ticket – our team answers 24/7.