How to Create systemd Service Files for a Bot or Script
To create systemd service units, write a file at /etc/systemd/system/name.service with your start command, run systemctl daemon-reload, then systemctl enable --now name. Your program then starts on boot and comes back automatically after a crash. This guide walks through a real example, a Python Telegram bot on Ubuntu 24.04 or Debian 12, and covers logs and the errors you are most likely to hit.
Why run a bot as a systemd service
Start a bot with python3 bot.py over SSH and it dies the moment you close the terminal. A systemd service gives you:
- Background execution with no open session.
- Run on boot after every server restart.
- Restart on failure, so a network hiccup does not take the bot down for good.
- Central logs in journald that you can filter by time or service.
Step 1: Create a user and set up the code
Do not run bots as root. Create a system user and a project folder:
# system user with no password and no login shell
sudo useradd --system --create-home --home-dir /opt/mybot --shell /usr/sbin/nologin mybot
# Python virtual environment and dependencies (aiogram as an example)
sudo apt install -y python3-venv
sudo -u mybot python3 -m venv /opt/mybot/venv
sudo -u mybot /opt/mybot/venv/bin/pip install aiogram
Put your code in /opt/mybot (for example bot.py) and the token in an environment file:
# secrets file readable only by root
sudo nano /etc/mybot.env
# BOT_TOKEN=123456:ABC...
sudo chmod 600 /etc/mybot.env
Step 2: Create systemd service unit file (example)
sudo nano /etc/systemd/system/mybot.service
Paste this:
[Unit]
Description=My Telegram bot
# wait until the network is up
After=network-online.target
Wants=network-online.target
# never give up restarting, even after repeated crashes
StartLimitIntervalSec=0
[Service]
Type=simple
User=mybot
WorkingDirectory=/opt/mybot
EnvironmentFile=/etc/mybot.env
ExecStart=/opt/mybot/venv/bin/python bot.py
# restart on any exit, after a 5 second pause
Restart=always
RestartSec=5
# send print() output to the journal immediately
Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target
What each key line does:
| Directive | Purpose |
|---|---|
After=network-online.target |
the bot does not start before the network and crash on its first API call |
User=mybot |
the process runs without root privileges |
EnvironmentFile |
keeps the token out of your code and Git |
ExecStart |
absolute path to the interpreter and script |
Restart=always, RestartSec=5 |
restart 5 seconds after any exit |
WantedBy=multi-user.target |
start during a normal boot |
Every directive is documented in the official systemd.service manual.
Step 3: Enable the service with systemctl
# reload unit files after creating or editing one
sudo systemctl daemon-reload
# start now and on every boot
sudo systemctl enable --now mybot
# check that it runs
systemctl status mybot
Look for Active: active (running).
Managing the service and reading logs
sudo systemctl restart mybot # restart after deploying new code
sudo systemctl stop mybot # stop it
sudo systemctl disable mybot # remove from boot
journalctl -u mybot -f # follow logs live
journalctl -u mybot -n 100 # last 100 lines
journalctl -u mybot --since "1 hour ago"
Run systemctl daemon-reload after every unit file edit, or systemd keeps using the old version.
Node.js, web apps, and scheduled scripts
For a Node.js bot, only the start line changes:
ExecStart=/usr/bin/node /opt/mybot/index.js
For jobs that run every hour or every night, use a systemd timer or cron instead of Restart=always. For web apps such as FastAPI, the service runs uvicorn behind Nginx; see deploy FastAPI.
Troubleshooting
What systemctl status shows |
Cause | Fix |
|---|---|---|
status=203/EXEC |
wrong ExecStart path or not executable |
use an absolute path; check ls -l and chmod +x |
status=200/CHDIR |
WorkingDirectory does not exist |
create the folder or fix the path |
status=217/USER |
the User= account does not exist |
create it with useradd |
ModuleNotFoundError in logs |
service uses system Python instead of the venv | point ExecStart at venv/bin/python |
start request repeated too quickly |
crash loop | read journalctl -u mybot, fix the bug, then systemctl reset-failed mybot |
| Empty logs | Python buffers output | add Environment=PYTHONUNBUFFERED=1 |
| Edits have no effect | missing daemon-reload |
sudo systemctl daemon-reload && sudo systemctl restart mybot |
systemd does not read ~/.bashrc. If your script relies on environment variables, set them with Environment= or EnvironmentFile=.
systemd, tmux, or a ready-made image
- systemd is the right tool for anything that must run 24/7 without you. Full walkthrough: run a Python script 24/7.
- tmux is handy for interactive work and long commands, but it will not restart a crashed process: tmux on a server.
- The "For bots" image on 0x.cloud ships with Python and Telegram and Discord libraries, Node.js with pm2, and a service that keeps your bot running. Step by step: deploy a Telegram bot in Python.
A single bot that calls an LLM fits the s1-2 plan (1 vCPU, 2 GB RAM) at $7.99 per month; a lightweight script fits s1-1 at $4.49. To try your bot first, use hourly billing: s1-2 is about $0.054725 an hour, and the unused amount returns to your balance when you delete it. Compare plans on VPS pricing.
Use run_command on my server to create a systemd service for the bot in /opt/mybot: run it as a dedicated user, restart on failure, load the token from an EnvironmentFile, enable it on boot, and show me the systemctl status output.
FAQ
Where do I put a systemd service file? Put your own units in /etc/systemd/system/name.service. Units shipped by packages live in /usr/lib/systemd/system; override those with systemctl edit instead of editing them.
What is the difference between systemctl enable and start? start runs the service now. enable makes it start on boot. systemctl enable --now does both in one command.
How do I create a systemd service without root? Use a user service: put the unit in ~/.config/systemd/user/, manage it with systemctl --user, and run loginctl enable-linger your_user so it keeps running after you log out.
How do I see the logs of a systemd service? Run journalctl -u name.service. Add -f to follow new lines live or -n 100 to show the last 100 lines.