Blog
3 min read

How to Host a Discord Bot 24/7 (Python or JavaScript)

A Discord bot only works while its program is running. Why your laptop isn't enough, the hosting options (a VPS, a container platform, a Raspberry Pi), how to keep the bot running with systemd or PM2, storing the token safely, and why free hosts are a poor fit for bots.

A Discord bot is just a program that stays connected to Discord. When the program stops — you close your laptop, your terminal, or it crashes — the bot goes offline. Hosting a bot means running that program somewhere that's always on, and restarting it when it fails.

Why bots are different from websites

A website waits for visitors. A Discord bot keeps an open connection to Discord's gateway all the time, so it can react instantly to messages. That means:

  • Hosts that sleep when idle don't work — the bot disappears until something wakes it.
  • Serverless functions don't fit a long-lived connection (unless your bot uses only slash commands via HTTP interactions, a less common setup).
  • It needs very little power. A small bot uses a few tens of megabytes of memory.

Your hosting options

Option Good for Watch out for
A small VPS Reliable 24/7, full control, cheap You set it up and keep it updated (What is a VPS?)
A container platform (Railway, Fly.io, Render background worker) Easy deploys from Git Usage-based billing, free tiers that sleep (Render vs Railway vs Fly)
A Raspberry Pi or old PC at home Free after hardware Home internet and power cuts
"Free bot hosting" sites Trying things out Unreliable, sometimes shady — they hold your token

Step 1: keep the token secret

Your bot token is a password for the bot. Anyone with it can control the bot — and abuse your server's members.

# .env  (add .env to .gitignore!)
DISCORD_TOKEN=your-token-here
# Python (discord.py)
import os
client.run(os.environ["DISCORD_TOKEN"])
// JavaScript (discord.js)
client.login(process.env.DISCORD_TOKEN)

If it ever ends up on GitHub, reset it in the Discord Developer Portal immediately — bots scan GitHub for leaked tokens within minutes. (I leaked an API key)

Step 2: put it on a server

ssh you@your-server
git clone https://github.com/you/mybot.git /srv/mybot
cd /srv/mybot

Install dependencies:

# Python
python3 -m venv .venv && .venv/bin/pip install -r requirements.txt

# JavaScript
npm ci --omit=dev

Create /srv/mybot/.env with the token. (How to SSH into a server)

Step 3: keep it running

With systemd (any language)

# /etc/systemd/system/mybot.service
[Unit]
Description=My Discord bot
After=network-online.target

[Service]
User=bot
WorkingDirectory=/srv/mybot
EnvironmentFile=/srv/mybot/.env
ExecStart=/srv/mybot/.venv/bin/python bot.py
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
sudo systemctl enable --now mybot
journalctl -u mybot -f

For a Node.js bot, ExecStart=/usr/bin/node index.js. (What is systemd?)

With PM2 (Node.js, also runs Python)

pm2 start index.js --name mybot
pm2 save && pm2 startup

(PM2 vs systemd)

Restart=always (or PM2) brings the bot back after crashes and reboots.

Step 4: updating it

cd /srv/mybot && git pull
.venv/bin/pip install -r requirements.txt   # or npm ci
sudo systemctl restart mybot

Storing data

If your bot keeps data — levels, settings, reminders — use a database (SQLite is fine for a small bot, Postgres for bigger ones), and back it up. Data in memory vanishes on every restart. (Is SQLite good enough for production?)

Running more than one bot

One small server can run many bots and other projects — each as its own service. That's usually cheaper than paying per bot on a platform.


EasySpawn gives you an always-on server where your bot runs as a managed service, restarted automatically, with your token kept in the environment and Claude Code on hand to add features. See how it works or join the waitlist.

Related: How to Host a Telegram Bot · How to Run a Python Script 24/7 · What Is systemd? · What Is a VPS?

Keep reading