Back to Blog

How to Run a Python Bot 24/7

How to Run a Python Bot 24/7
Command of the Day
python3 -m venv venv

What it does: Creates an isolated Python virtual environment directory to manage project dependencies independently from system packages.

Python is renowned for its clean syntax and extensive ecosystem of libraries for building Discord bots (discord.py, disnake), Telegram bots (python-telegram-bot), web scrapers (BeautifulSoup, Playwright), and automated crypto/stock trading bots.

In this guide, we walk through setting up a virtual environment, managing dependencies, and hosting a Python bot 24/7 on your RivinCloud VPS.


1. Step 1: Install Python and Development Tools

On Ubuntu/Debian, install Python 3, pip, and venv:

sudo apt update
sudo apt install -y python3 python3-pip python3-venv git

Verify installed versions:

python3 --version
pip3 --version

2. Step 2: Set Up Project & Virtual Environment

Never install project pip packages globally across your operating system. Always create an isolated virtual environment:

mkdir -p ~/my-python-bot
cd ~/my-python-bot

# Create virtual environment named 'venv'
python3 -m venv venv

# Activate virtual environment
source venv/bin/activate

(Your prompt will now show (venv) at the beginning).

Install your dependencies:

pip install discord.py python-dotenv

Save package requirements:

pip freeze > requirements.txt

3. Step 3: Configure Environment Variables

Create .env:

nano .env

Add your bot credentials:

DISCORD_TOKEN=your_private_python_bot_token_here

Sample Python script (bot.py):

import os
import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'✅ Python Bot connected as {client.user}')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content == '!hello':
        await message.channel.send('Hello from Python 24/7 on RivinCloud VPS!')

client.run(TOKEN)

Command of the Day: python3

The python3 command invokes the Python 3 interpreter.

Syntax

python3 [options] [script.py]

Practical Examples

# 1. Execute script using active virtual environment interpreter directly
./venv/bin/python3 bot.py

# 2. Check installed modules
./venv/bin/python3 -m pip list

4. Step 4: Keep Python Bot Online 24/7 with Systemd

Create a dedicated systemd service to manage your Python bot:

sudo nano /etc/systemd/system/pythonbot.service

Paste the following configuration:

[Unit]
Description=24/7 Python Bot Daemon
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu/my-python-bot
ExecStart=/home/ubuntu/my-python-bot/venv/bin/python3 /home/ubuntu/my-python-bot/bot.py
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

Reload systemd daemon, enable on boot, and start service:

sudo systemctl daemon-reload
sudo systemctl enable --now pythonbot

Check live service status:

sudo systemctl status pythonbot

Common Mistakes

  • Forgetting source venv/bin/activate: Installing packages without an active virtual environment leads to externally-managed-environment errors on newer Ubuntu releases.
  • Relative file paths inside Python scripts: Opening local files (e.g. open('data.json')) fails if the working directory changes. Use absolute paths based on os.path.dirname(__file__).

Troubleshooting

  • Check Live Systemd Logs:
    sudo journalctl -u pythonbot -f
    

Best Practices

  • Always freeze requirements using pip freeze > requirements.txt.
  • Use systemd or PM2 (via pm2 start bot.py --interpreter ./venv/bin/python3) for continuous uptime.

Frequently Asked Questions

Can I run Python bots with PM2 instead of Systemd?

Yes! Install PM2 and run: pm2 start bot.py --name "python-bot" --interpreter ./venv/bin/python3


Conclusion

Hosting Python bots with virtual environments and systemd services provides rock-solid 24/7 uptime on your RivinCloud VPS!