How to Run a Python Bot 24/7 on a VPS
The bot works in an SSH window and dies the moment the connection closes. A terminal session is not a service manager.
Some links on this site are affiliate links. If you buy through them, we earn a commission at no extra cost to you. We only recommend tools we would deploy ourselves.
To run a Python bot 24/7, install it under a restricted user and let systemd supervise the process. nohup is a sticky note; systemd is the operating system doing its job.
Python bot VPS setup
sudo useradd --system --create-home --shell /usr/sbin/nologin botuser
sudo mkdir -p /opt/mybot
sudo cp -r . /opt/mybot/
sudo chown -R botuser:botuser /opt/mybot
sudo -u botuser python3 -m venv /opt/mybot/.venv
sudo -u botuser /opt/mybot/.venv/bin/pip install -r /opt/mybot/requirements.txt
Store secrets in /etc/mybot.env, readable only by root. Create /etc/systemd/system/mybot.service:
[Unit]
Description=My Python bot
After=network-online.target
Wants=network-online.target
[Service]
User=botuser
WorkingDirectory=/opt/mybot
EnvironmentFile=/etc/mybot.env
ExecStart=/opt/mybot/.venv/bin/python /opt/mybot/bot.py
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now mybot
sudo journalctl -u mybot -f
Choose Vultr, DigitalOcean, or a memory-heavy Contabo plan after reading the bot hosting ranking.
Run the Python bot without downtime surprises
A restart loop means the real exception is in journalctl. Missing secrets mean the environment file permissions or variable names are wrong. Duplicate work after restart needs idempotency in the bot; a service manager cannot make the application safe.
Reboot the VPS once and confirm the service returns before calling the deployment finished.
Vultr
Fast, cheap, global cloud instances.
DigitalOcean
Developer-first cloud. The default Droplet in every GitHub tutorial.
Contabo
The king of cheap RAM-heavy VPS.
Found the fix? The tool that ends the problem is one click away.
The Stack