On this page · 10 min
You run python bot.py, the bot answers in your server, and then you close your laptop and it goes grey in the member list. To host a Discord bot so that this stops happening, it helps to know why it goes offline before choosing where it runs.
Why a Discord bot goes offline
A bot goes offline when its process stops, because the bot is the client. A website waits for requests to come in, whereas a bot opens a connection to Discord, called the Gateway, and keeps it open so Discord can push messages and commands down it. Discord's docs describe these connections as "persistent WebSockets" that your app has to open and maintain (the Gateway docs).
To show it is still there, the bot sends a small heartbeat message at an interval Discord sets; the example in the docs is every 45 seconds. When your laptop sleeps, the heartbeats stop, Discord closes the connection, and a few minutes later the bot shows as offline.
So a bot built on the Gateway, as most discord.py and discord.js bots are, needs a process that runs all the time, and anything that sleeps or has a time limit won't work. It needs no public URL and no open port, because the connection goes out from the bot. Short network drops are normal, and libraries such as discord.py reconnect by themselves, which leaves the machine and the process as the two things you have to keep alive.
Where to host Discord bots
The three kinds of place differ in who keeps the machine on and who restarts the bot.
| Your computer or a Raspberry Pi | A bot host with a panel | A Linux machine in the cloud | |
|---|---|---|---|
| Who keeps it on | You, your power and your internet | The host | The provider |
| How the code gets there | It's already there | Upload, or connect a GitHub repo | git clone from a shell |
| What restarts a crashed bot | A service you set up | The host | A service you set up |
Hosting the bot locally, on your own computer, is right while you build it, and wrong once the bot has to be online while your laptop is in a bag. A Raspberry Pi or an old computer at home works for a hobby bot. It is a Linux machine, so the steps below work on it unchanged, and the bot then depends on your home's power and internet connection.
A bot host runs the bot for you in a container, which is a boxed-off process on a shared server. You upload files or connect a repository and paste in the token, and the host restarts the bot if it crashes, which makes this the least work of the three. Read a free plan's terms before you rely on it. Bot-Hosting.net, for example, says its free plan has 256 MB of RAM and needs a "manual renewal every 4 days" (its plans), and other hosts have similar rules, so a bot whose renewal you forgot goes offline.
A Linux machine in the cloud is a virtual machine (VM) that a provider keeps running for you. You can get one on boxd or as a VPS (virtual private server) from any hosting provider. With a VPS you first pick a plan and an operating system image and add an SSH key in a panel, and AWS is that route with more to decide: an EC2 instance (AWS's name for a VM) also needs an instance type and a firewall rule that allows SSH. On any of them you install what you like, and in exchange you set up the restart behaviour yourself, which is the seven steps below.
Two places people try first don't do the job. GitHub stores your code but doesn't run it: GitHub Pages serves static sites only, and a GitHub Actions job is stopped after 6 hours, with GitHub's terms ruling out work unrelated to building, testing and releasing the project. Heroku ended its free plans on November 28, 2022. Its paid Eco plan can run a bot as a worker dyno (a dyno is Heroku's name for a running process), but only if the app has no web dyno, because an idle web dyno sleeps after 30 minutes and takes the worker down with it (Eco dyno hours). Our guide to Heroku alternatives covers where small apps go now.
How to host a Discord bot on a Linux machine
At the end of these steps the bot starts when the machine boots, restarts within seconds of a crash, and writes its output to the system log. You need a bot that already runs on your computer, its code in a Git repository, and its token from the Discord Developer Portal. The examples use Python and a file called bot.py; step 5 shows what changes for Node.js.
-
Get a machine and open a shell on it. On a VPS, an EC2 instance or a Raspberry Pi, create or switch on the machine, connect with
sshand go to step 2. On boxd, run these on your laptop:Terminal curl -fsSL https://boxd.sh/downloads/install.sh | sh boxd auth login boxd machine new discord-bot --vcpu 1 --auto-hibernate-timeout=0 boxd connect discord-botThe first two lines install the CLI and sign you in. The third creates a machine of the smallest size, 1 vCPU and 4 GiB of memory, and the fourth opens a shell on it.
The
--auto-hibernate-timeout=0flag applies the rule from earlier, that nothing under the bot may sleep. By default a boxd machine hibernates after 4 hours without network traffic. A connected bot's heartbeats count as traffic, so it would usually stay awake, but if the bot were ever stopped for that long the machine would hibernate, and nothing inside it would wake it again. Setting the timeout to 0 turns hibernation off. Standby, the other idle timer, is off by default (suspend, resume and hibernate). -
Put the code on the machine.
Terminal git clone https://github.com/you/my-bot.git ~/bot cd ~/botThis copies your repository into a folder called
botin your home directory and moves you into it. A private repository needs credentials on the machine first; a read-only SSH key added to that one repository is enough. Ifgitis missing, install it withsudo apt install git. -
Install the bot's dependencies.
Terminal python3 -m venv .venv .venv/bin/pip install -r requirements.txtThe first line creates a virtual environment, a private folder of Python packages for this bot, and the second installs your requirements into it, ending with a "Successfully installed" line. If the first fails on Ubuntu or Debian, run
sudo apt install python3-venvand try again. -
Store the token outside the code.
Terminal sudo touch /etc/discord-bot.env sudo chmod go= /etc/discord-bot.env sudo nano /etc/discord-bot.envThe first two lines create an empty file that only root can read. In the editor, add one line,
DISCORD_TOKEN=followed by your token, then save. Your bot should read the token from that environment variable, which in Python isos.environ["DISCORD_TOKEN"]. Discord's own guide says to "never share your token or check it into any kind of version control" (getting started), because anyone who has it can act as your bot. -
Describe the bot as a service. Create the file
/etc/systemd/system/discord-bot.servicewithsudo nanoand paste this:TOML [Unit] Description=Discord bot After=network-online.target Wants=network-online.target StartLimitIntervalSec=10min StartLimitBurst=10 [Service] User=boxd WorkingDirectory=/home/boxd/bot EnvironmentFile=/etc/discord-bot.env Environment=PYTHONUNBUFFERED=1 ExecStart=/home/boxd/bot/.venv/bin/python bot.py Restart=always RestartSec=5 [Install] WantedBy=multi-user.targetUserand the two paths name the user and folder the bot runs from.boxdis the user on a boxd machine, so anywhere else replace it with your own username in all three lines.EnvironmentFilehands the token to the bot.PYTHONUNBUFFERED=1makes Python write its output to the log straight away.Restart=alwayswithRestartSec=5is what keeps the bot online: whenever the process exits, for any reason, systemd starts it again 5 seconds later (the systemd manual).The two
StartLimitlines stop that from going on forever: after 10 starts within 10 minutes, systemd gives up and marks the service failed. That matters because Discord allows a bot 1000 logins in 24 hours and resets its token beyond that, so a bot that crashes on every start must not retry all night. Docker and pm2 can do the same job, but systemd is already on the machine.For a Node.js bot, install with
npm ciin step 3 and setExecStartto the output ofwhich nodefollowed by your entry file, for example/usr/bin/node index.js. -
Start it, now and on every boot.
Terminal sudo systemctl daemon-reload sudo systemctl enable --now discord-bot systemctl status discord-botdaemon-reloadmakes systemd read the new file.enablemakes the service start at boot, and--nowalso starts it right away. The status should showactive (running), and in Discord the bot turns green. Check the status again after half a minute, because a bot that crashes on start can look healthy for a second or two. If the state is thenactivating (auto-restart)orfailed, the bot is crashing, and the next step shows you why. -
Prove it restarts.
Terminal journalctl -u discord-bot -n 20 sudo systemctl kill --signal=SIGKILL discord-bot sleep 10 systemctl status discord-botThe first line prints the bot's last 20 log lines, which is where its errors go from now on. The second kills the process the way a crash would, and the third waits for the restart. The status then shows
active (running)again with a new process ID. You can close your laptop now, and the bot stays online.If a crash loop ever trips the start limit, fix the bug, then run
sudo systemctl reset-failed discord-botandsudo systemctl start discord-bot.
To ship a change later, run git pull in ~/bot and then sudo systemctl restart discord-bot.
Which one to pick
Pick by how much you want to look after:
- If you're still building the bot, run it on your own computer.
- If it's a hobby bot and you have a Raspberry Pi at home, use the Pi with the steps above, and accept that a power cut takes the bot down with it.
- If you don't want to touch Linux, use a bot host. It does the setup and the restarts for you, which a machine of your own doesn't. Check a free plan's memory and its renewal rule.
- If you want full control, or the bot has outgrown a panel, use a Linux machine in the cloud. Choose boxd if you don't want to set up a server first: once the CLI is installed, one command creates the machine and
boxd connectopens a shell on it with no SSH keys or config to arrange, and the machine has no limit on how long it runs. Choose a plain VPS if you already run servers and have your keys and habits in place; it also has no idle timer to turn off. Choose EC2 if the rest of your work is already on AWS. - If you were counting on GitHub or a free Heroku plan, neither will keep a bot online.
What to do next
If the bot keeps its data in a file, such as a SQLite database, that file now lives on one machine, so copy it somewhere else on a schedule. A provider's disk backups do this for the whole machine once you switch them on; on boxd, scheduled disk backups are kept 7 days by default.
If you haven't got a machine yet and want the one from step 1, install the CLI:
curl -fsSL https://boxd.sh/downloads/install.sh | shThen sign in with boxd auth login and create the machine as step 1 shows.
Frequently asked
- How do you host a Discord bot 24/7?
- Run it on a computer that never sleeps, with something that restarts it after a crash. On a Linux machine that means a systemd service with Restart=always, enabled so it starts at boot, and a start limit so a crash loop can't use up Discord's 1000 logins in 24 hours. A bot host with a panel does the same for you, within its plan's rules.
- Can you host a Discord bot on GitHub?
- No. GitHub stores the code but doesn't keep it running: GitHub Pages serves static sites only, a GitHub Actions job on a GitHub-hosted runner is stopped after 6 hours, and GitHub's terms rule out using Actions for work unrelated to building, testing and releasing the project.
- Can you host a Discord bot on Heroku for free?
- No. Heroku stopped offering free plans from November 28, 2022. On the paid Eco plan a bot can run as a worker dyno, but only if the app has no web dyno, because an idle web dyno sleeps after 30 minutes and takes the worker with it.
- Can you host a Discord bot locally?
- Yes, while your computer is on and connected. A bot is online only while its program holds a connection to Discord, so it goes offline a few minutes after your computer sleeps. That is fine while you build the bot and not once it has to be up all the time.
- Can you host a Discord bot on a Raspberry Pi?
- Yes. A Raspberry Pi is a Linux machine, so the same steps work: clone the code, install its dependencies, keep the token in a file only root can read, and run the bot as a systemd service. The bot depends on your home's power and internet connection.
- How do you host a Discord bot on a VPS or AWS?
- Connect over SSH, clone the bot's repository, install its dependencies, store the token in an environment file, and create a systemd service that starts the bot at boot and restarts it when it exits. On AWS you first launch an EC2 instance with a key pair and a rule that allows SSH.



