How to Host a Discord Bot 24/7
How to host a Discord bot 24/7: why free options fall short, running a bot on a VPS, auto-restart with pm2 or systemd, and keeping your token safe.

Contents
The most reliable way to host a Discord bot 24/7 is to run it on a server that is always on, not on your own computer, and to use a process manager so it comes back up automatically if it crashes or the server reboots. A small Linux VPS, your bot's code and a tool like pm2 or systemd are all you need.
This guide explains why free options fall short, then walks you through setting up a Node.js or Python bot on a VPS, starting it automatically and keeping your token safe.
Why your own PC or a free host isn't enough
A Discord bot keeps a persistent connection (the gateway) open to Discord's servers. The moment that connection drops, the bot shows as offline and stops answering commands.
- On your own computer, the bot only runs while the computer is on. An update restart, a power cut or a dropped connection stops the bot too.
- Free hosting services usually limit resources, put apps to sleep after a period of inactivity or cap monthly running hours. The "keep-alive ping" tricks suggested to get around this often break those services' terms.
- Shared web hosting is built for websites, not apps that hold a connection open around the clock.
A small VPS gives your bot dedicated RAM, a fixed IP address and full control. You can run several bots, a database and the bot's web dashboard on the same server. We explain how a VPS works in What is a VPS?
Step 1: Prepare the server
Connect to a VPS running Ubuntu or Debian over SSH. If SSH is new to you, our guide What is SSH? walks you through connecting and securing the server.
It is safer to run the bot as a separate user rather than root:
adduser bot
su - bot
Step 2: Install the runtime
For Node.js bots (discord.js), install a current Node.js LTS release. The easiest way is nvm; get the install command from nvm's official repository, then:
nvm install --lts
node -v
For Python bots (discord.py), Python 3 ships with Ubuntu and Debian. Install the package for virtual environments and create one in the bot's folder:
sudo apt install python3-venv
python3 -m venv venv
source venv/bin/activate
Step 3: Get the bot's code onto the server
If your code is in a Git repository, the easiest way is to clone it:
git clone https://github.com/you/my-bot.git
cd my-bot
Then install dependencies: npm install for Node.js, pip install -r requirements.txt for Python.
Don't put the token in your code
Your bot token is your bot's password. Instead of writing it in the code, keep it in a .env file:
DISCORD_TOKEN=your-token-here
Add the file to .gitignore so it is never pushed to a Git repository by accident. Tokens pushed to public repositories are found and abused within minutes. Also make the file readable only by the bot user:
chmod 600 .env
Start the bot once by hand (node index.js or python bot.py), check that it shows as online in Discord, then stop it with Ctrl+C.
Step 4: Keep the bot running 24/7
When you run the bot from the terminal, it stops as soon as you close the SSH session. To keep it running, use a process manager.
Option A: pm2 (recommended for Node.js bots)
pm2 runs Node.js apps in the background, restarts them if they crash and keeps their logs:
npm install -g pm2
pm2 start index.js --name my-bot
pm2 save
pm2 startup
pm2 startup prints a command you need to run so pm2 starts automatically after a reboot; copy and run it. The commands you'll use most:
pm2 logs my-bot: follows the bot's logs live.pm2 restart my-bot: restarts the bot after you update the code.pm2 ls: lists running processes and their memory use.
Option B: systemd (recommended for Python bots)
With systemd, Linux's built-in service manager, you can run the bot as a service without installing anything extra. Create /etc/systemd/system/my-bot.service:
[Unit]
Description=My Discord bot
After=network-online.target
Wants=network-online.target
[Service]
User=bot
WorkingDirectory=/home/bot/my-bot
EnvironmentFile=/home/bot/my-bot/.env
ExecStart=/home/bot/my-bot/venv/bin/python bot.py
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable --now my-bot
Follow the logs with journalctl -u my-bot -f. Restart=always brings the bot back 5 seconds after a crash, and enable starts it automatically after a reboot.
Step 5: Keep the bot healthy
- To update the code, run
git pull, reinstall dependencies if needed and restart the bot (pm2 restart my-botorsudo systemctl restart my-bot). - Keep libraries current. The Discord API changes from time to time, and current versions of discord.js and discord.py keep up with it.
- Catch errors. An uncaught error can take the whole bot down. Catch and log errors in your command handlers.
- Back up your database. If the bot stores settings and user data in SQLite, PostgreSQL or MongoDB, back it up regularly.
- Prepare for sharding as you grow. Discord requires bots in more than 2,500 servers to use sharding. Both discord.js and discord.py have built-in tools for it.
Hosting a Discord bot at Nodesty
A Discord bot doesn't need a big server; what matters is staying online and answering commands quickly. Nodesty's AMD Ryzen 9 9950X VPS plans offer high single-core speed, NVMe storage and DDoS protection in Frankfurt and New York, and our AMD EPYC 7763 VPS plans give you more RAM for the price in Frankfurt. Ubuntu and Debian are available at install, and your server is set up automatically once payment is confirmed.
Frequently Asked Questions
How much RAM does a Discord bot need?
A simple moderation or command bot in a few servers usually runs in a few hundred MB. Music bots, bots that keep large caches and bots serving thousands of servers need more. If you'll run several bots and a database on the same VPS, 2–4 GB is a comfortable start.
Can I run my bot 24/7 on my own computer?
Technically yes, but the bot goes offline whenever your computer shuts down, restarts or loses its connection. Keeping a computer on all the time can also cost about as much in electricity as a small VPS.
What should I do if my bot token leaks?
Reset the token immediately in the Discord Developer Portal, under your bot's Bot tab (Reset Token). The old token stops working at once. Then put the new token in the .env file on the server, restart the bot and find out how the token leaked.
Should I use pm2 or systemd?
pm2 is convenient for Node.js bots, with one-command start, log viewing and automatic restarts. For Python bots, or if you'd rather not install extra tools, Linux's built-in service manager, systemd, is enough. Both start the bot again automatically after a server reboot.
Why can't my bot read messages?
Discord treats access to message content as a privileged intent (Message Content Intent). Turn it on under your bot's Bot tab in the Developer Portal and enable the matching intent in your code. Bots in more than 100 servers need Discord's approval for it.
Will a Discord bot run on web hosting?
Node.js and Python support on shared hosting is designed for web apps that answer requests. A Discord bot keeps a connection to Discord open all the time, so a VPS is the better fit for running it without interruption.
About the author
Nodesty Team · Infrastructure & Support
The Nodesty team runs our servers, network and DDoS protection day to day and answers customer support requests around the clock. These guides come from that work.
Related articles

How to Upgrade Ubuntu Server to the Next LTS Release
How to upgrade an Ubuntu server to the next LTS release: backups, preparation, do-release-upgrade, SSH access, post-upgrade checks and a clean install.

What Is a Load Balancer and How Does It Work?
What is a load balancer and what does it do? Balancing methods, health checks, session handling, load balancing with Nginx, HAProxy and when you need one.

How to Deploy a Laravel App to a VPS
How to deploy Laravel to a server: PHP-FPM, Composer, .env, the database, file permissions, Nginx, queue workers, the scheduler, SSL and shipping updates.