Deploy a Python Telegram bot with systemd, the robust way
The basic systemd unit keeps a bot alive. The production unit also logs cleanly, shuts down gracefully, backs off after repeated crashes and sandboxes the process so a bug cannot wander the filesystem. Here is that unit, explained directive by directive.
Why systemd over nohup, screen or tmux
Running a bot in screen or with nohup works until the server reboots, the session dies, or the process crashes at night with nobody watching. systemd solves all three: it starts on boot, restarts on failure, and gives you real logging and process control. It is already on every Ubuntu box — no extra tools. If you host with Docker instead, a container with a restart policy plays the same role.
The production unit file
sudo nano /etc/systemd/system/mybot.service
[Unit]
Description=My Telegram bot (python-telegram-bot)
After=network-online.target
Wants=network-online.target
StartLimitIntervalSec=300
StartLimitBurst=5
[Service]
Type=simple
User=deploy
Group=deploy
WorkingDirectory=/home/deploy/mybot
EnvironmentFile=/home/deploy/mybot/.env
ExecStart=/home/deploy/mybot/venv/bin/python -u bot.py
Restart=always
RestartSec=5
TimeoutStopSec=20
KillSignal=SIGINT
# --- sandboxing ---
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=read-only
ReadWritePaths=/home/deploy/mybot
[Install]
WantedBy=multi-user.target
The directives that matter
Restart with a limit
Restart=always revives the bot after any exit. StartLimitBurst=5 / StartLimitIntervalSec=300 stop a hard-failing bot from restarting forever in a tight loop — after five crashes in five minutes systemd gives up and marks it failed, so you get an alert instead of a spin.
Graceful shutdown
KillSignal=SIGINT sends Ctrl-C rather than a hard kill, letting python-telegram-bot finish the current update and close cleanly. TimeoutStopSec=20 gives it time before a forced stop.
Unbuffered logging
python -u disables output buffering so log lines reach journald immediately — otherwise messages can sit in a buffer and appear late or not at all on a crash.
Sandboxing
ProtectSystem=strict makes the whole filesystem read-only except the paths you allow; ReadWritePaths re-opens just the bot's directory. NoNewPrivileges and ProtectHome=read-only shrink what a compromised bot can touch. Cheap, strong hardening — the same spirit as Docker security hardening.
Manage and inspect
sudo systemctl daemon-reload
sudo systemctl enable --now mybot
sudo systemctl status mybot
journalctl -u mybot -f
journalctl -u mybot -p err --since today # errors only
Deploying an update
cd ~/mybot && git pull
source venv/bin/activate && pip install -r requirements.txt
sudo systemctl restart mybot
journalctl -u mybot -n 30
Because the token lives in .env and code in git, a deploy is pull-install-restart — repeatable and boring, which is what you want.
Rock-solid uptime for a tiny cost
systemd keeps the process up; a reliable host keeps the server up. Inferno's $3.49 plan gives a stable, always-on home across eight EU + Asia locations.
What to do next
- Webhook vs polling — does this bot need a public URL?
- Node.js + PM2 — the JavaScript equivalent.
- Monitoring — alert when the unit enters a failed state.
Author: Riku Lehtinen — Linux sysadmin, Tampere. Last tested: 2026-05-18 on systemd 249, python-telegram-bot 21, Ubuntu 22.04.