Running a Discord Bot 24/7 on a VDS (pm2 and systemd)

Running a Discord Bot 24/7 on a VDS (pm2 and systemd)

Keep your Discord bot online around the clock on a VDS: installing Node.js, defining a service with pm2 or systemd, automatic restarts, log management and memory planning.

White Bilişim

Running a Discord Bot 24/7 on a VDS

Everything looks fine while your bot runs on your own machine, right up until you shut that machine down. This guide covers running the bot as a service on a VDS, restarting it automatically after a crash, and keeping the logs under control.

Why a VDS?

ProblemYour own machineVDS
Machine shuts downBot goes offlineKeeps running
Power cutDowntimeRedundant power
IP changesConnection issuesStatic IP
Internet outageBot goes offlineRedundant network

1. Preparing the Server

sudo apt update && sudo apt upgrade -y
sudo adduser --disabled-password --gecos "" bot
sudo su - bot

Do not run the bot as root. A dedicated user is both safer and tidier.

2. Installing Node.js

The Node.js version in your distribution’s repository is usually well behind. Use nvm to install exactly the version you want:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
source ~/.bashrc
nvm install 22
node -v

3. Getting the Bot Onto the Server

git clone https://github.com/username/your-bot.git ~/bot
cd ~/bot
npm ci --omit=dev

Never hardcode the token. Keep it in a .env file and restrict its permissions:

echo "DISCORD_TOKEN=..." > ~/bot/.env
chmod 600 ~/bot/.env

⚠️ If you accidentally pushed a token to a repository, regenerate it immediately from the Discord developer portal. Tokens that land in public repositories get scraped and abused within minutes.

4. Option A: Running With pm2

pm2 is a convenient process manager for Node.js projects:

npm install -g pm2
pm2 start index.js --name discord-bot
pm2 save
pm2 startup

That last command installs the service needed to bring the bot back up after a server reboot. Handy commands:

pm2 logs discord-bot      # live logs
pm2 restart discord-bot   # restart
pm2 monit                 # memory and CPU monitoring

Automatic restart as a defence against memory leaks:

pm2 start index.js --name discord-bot --max-memory-restart 500M

5. Option B: Running With systemd

If you would rather not add another dependency, systemd does the job on its own:

sudo nano /etc/systemd/system/discord-bot.service
[Unit]
Description=Discord Bot
After=network.target
 
[Service]
Type=simple
User=bot
WorkingDirectory=/home/bot/bot
EnvironmentFile=/home/bot/bot/.env
ExecStart=/home/bot/.nvm/versions/node/v22.0.0/bin/node index.js
Restart=always
RestartSec=10
 
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now discord-bot
sudo journalctl -u discord-bot -f

💡 Restart=always brings the bot back up after every crash. Do not set RestartSec too low: a bot stuck in a crash loop can restart dozens of times per second and hit Discord’s rate limits.

6. Memory Planning

Bot typeTypical memoryNote
Text commands150-400 MBThe most common scenario
Music bot500 MB to 1.5 GBAudio encoding overhead
Bot with a database+200-500 MBIf PostgreSQL or Redis runs on the same server
Sharded bot+150 MB per shardOnce you pass 2,500 servers

7. Log Management

Log files fill the disk over time. If you use pm2, set up log rotation:

pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:retain 7

With systemd, journald already rotates logs for you, and you can adjust the limits in /etc/systemd/journald.conf.

8. Security

  • Log into SSH with a key instead of a password, and disable password authentication
  • Keep only the SSH port open in the firewall (the bot makes outbound connections, so it needs no inbound port)
  • Apply system updates regularly
sudo ufw allow OpenSSH
sudo ufw enable

Conclusion

Running a Discord bot without interruption does not require complex infrastructure. A properly configured process manager and enough memory will do it. pm2 is the quickest way to get started, while systemd suits anyone who prefers not to add extra dependencies.

👉 For a plan that matches your bot’s scale, see our Discord bot VDS page, or the Telegram bot and automation VDS page if you plan to run a Telegram bot or scheduled scripts on the same server.