Guides
How to host a Discord bot 24/7
How to keep a discord.js or discord.py bot online around the clock, on bot hosting or a VPS of your own, and restart it by itself if it crashes.
Why your bot goes offline #
When you run a bot from your own computer, it stops when the computer sleeps, restarts or loses its connection, and when you close the terminal. To stay online all the time, the bot has to run on a server that is always on, and something has to start it again if it crashes.
There are two ways to do that:
| Bot hosting | A VPS | |
|---|---|---|
| Setup | Upload your files, add the token, press Start | Install Node.js or Python and set up a service |
| Restarts on crash | Yes, by itself | Yes, once you set it up (below) |
| What else it runs | Just the bot | Anything: other bots, a database, a website |
| Skill needed | None beyond writing the bot | Comfortable typing commands over SSH |
Option 1: bot hosting #
The quickest way. Discord bot hosting runs one bot per plan with Node.js 24 or Python 3.13, and restarts it if it crashes. You upload your files, paste your token and press Start from the bot's page in the Portal. The steps are in Upload your bot and Token and variables.
Option 2: on your own VPS #
On a VPS you set the bot up yourself, once, as a systemd service. systemd is the part of Linux that starts programs when the server boots and starts them again when they stop.
These steps are for Ubuntu or Debian, logged in as your own user. If the server is new, do First steps on a new Linux VPS first.
1. Install Node.js or Python #
For a discord.js bot, install Node.js:
curl -fsSL https://deb.nodesource.com/setup_24.x | sudo -E bash -
sudo apt install -y nodejs
node --version
For a discord.py bot, Python is already on the server. Add what you need to make a virtual environment:
sudo apt install -y python3 python3-venv
2. Put the bot's files on the server #
Make a folder and copy the bot into it, with git clone if it is in a repository, or with an SFTP app such as WinSCP or
FileZilla:
mkdir -p ~/mybot && cd ~/mybot
git clone https://github.com/you/your-bot.git .
Then install its packages. For Node.js:
npm install --omit=dev
For Python:
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
3. Keep the token out of the code #
Put the token in a file of its own that only you can read, instead of in the code:
echo 'DISCORD_TOKEN=paste-your-token-here' > ~/mybot/.env
chmod 600 ~/mybot/.env
Your bot reads it from the environment: process.env.DISCORD_TOKEN in Node.js, os.environ["DISCORD_TOKEN"] in Python.
Caution
Never put a token in code you push to GitHub. Bots scan public repositories for tokens within minutes. If one leaks, reset it in the Discord Developer Portal straight away.
4. Make it a service #
Create the service file:
sudo nano /etc/systemd/system/mybot.service
Paste this in, with your user name instead of alex. For a Node.js bot started with index.js:
[Unit]
Description=My Discord bot
After=network-online.target
Wants=network-online.target
[Service]
User=alex
WorkingDirectory=/home/alex/mybot
EnvironmentFile=/home/alex/mybot/.env
ExecStart=/usr/bin/node index.js
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
For a Python bot started with main.py, change the ExecStart line to:
ExecStart=/home/alex/mybot/.venv/bin/python main.py
Save, then start it and have it start on every boot:
sudo systemctl daemon-reload
sudo systemctl enable --now mybot
Restart=always is what keeps the bot online: if it crashes, systemd starts it again five seconds later.
5. Check on it #
systemctl status mybot # is it running?
journalctl -u mybot -f # its output, live (Ctrl+C to stop watching)
sudo systemctl restart mybot # after you change its code
How much memory a bot needs #
Most small bots use well under 256 MB. A bot in many servers, one that caches a lot, or one that plays music needs more.
Watch its memory for a few days (systemctl status mybot shows it) and pick a plan with room to spare.
Next steps #
- See the plans on Discord bot hosting or VPS hosting.
- Not sure which one? VPS vs shared hosting explains what a VPS gives you.