TutorialsAugust 4, 20264 min read

How to Keep a Discord Bot Online with PM2

Use PM2 to keep your Node.js Discord bot running 24/7 on a VPS: auto-restart on crash, start on boot, log management, and zero-downtime restarts.

NBy Nxeon

PM2 is the go-to process manager for keeping a Node.js Discord bot online 24/7. It restarts your bot if it crashes, brings it back after a server reboot, manages logs, and does it all with a couple of commands. This guide sets it up properly. If you haven't deployed your bot yet, start with how to host a Discord bot on a VPS 24/7.

Why PM2

  • Auto-restart on crash — an unhandled error won't leave your bot offline.
  • Start on boot — survives server reboots automatically.
  • Log management — captures stdout/stderr with timestamps.
  • Monitoring — live CPU/RAM view with pm2 monit.

Step 1: Install Node.js and PM2

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
sudo npm install -g pm2
pm2 -v

Step 2: Start your bot under PM2

From your bot's directory:

cd ~/your-bot
npm install
pm2 start index.js --name discord-bot

--name gives it a friendly label. Check it's running:

pm2 status
The Nxeon game-server control panel — live console, player slots, and TPS
The Nxeon game-server control panel — live console, player slots, and TPS

Step 3: Make it start on boot

This is the step people forget — without it, the bot won't come back after a reboot. Generate and install the startup script:

pm2 startup

PM2 prints a sudo command tailored to your system — copy and run it. Then save the current process list so it's restored on boot:

pm2 save

Now reboot and confirm:

sudo reboot
# reconnect, then:
pm2 status

Step 4: Managing the bot

Daily commands you'll use:

pm2 logs discord-bot        # tail logs
pm2 restart discord-bot     # restart after a code change
pm2 stop discord-bot        # stop without removing
pm2 delete discord-bot      # remove from PM2
pm2 monit                   # live CPU/RAM dashboard

To deploy an update:

cd ~/your-bot
git pull
npm install
pm2 restart discord-bot

Step 5: Handle crashes and memory sensibly

PM2 restarts on crash by default. Add guards so a broken loop doesn't restart forever, and cap memory so a leak restarts the bot cleanly:

pm2 start index.js --name discord-bot \
  --max-memory-restart 300M \
  --restart-delay 5000

--max-memory-restart restarts if RAM crosses the limit; --restart-delay avoids hammering restarts on a persistent error.

Step 6: Use an ecosystem file (optional but tidy)

For repeatable config, create ecosystem.config.js:

module.exports = {
  apps: [{
    name: "discord-bot",
    script: "index.js",
    max_memory_restart: "300M",
    env: { NODE_ENV: "production" }
  }]
};

Then pm2 start ecosystem.config.js. This keeps your launch settings in version control alongside the bot.

PM2 vs systemd

PM2 is quick and log-friendly; a systemd service is more "native" and has no extra dependency. For a Python bot, systemd is often cleaner — see how to host a Python Discord bot on a Linux VPS. Either way, run the bot as a non-root user on a hardened box — securing your first Linux VPS: 10 steps.

Running several bots on one server

PM2 shines when you host multiple bots. Start each with its own name and they're managed independently:

pm2 start mod-bot/index.js --name mod-bot
pm2 start music-bot/index.js --name music-bot
pm2 save

pm2 list shows them all; pm2 restart music-bot touches only one. Pair this with a separate project folder (and ideally a separate Linux user) per bot for clean isolation.

Log rotation so disks don't fill

By default PM2 logs grow forever. Install the log-rotate module:

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

This caps each log at 10 MB and keeps a week of history.

Troubleshooting

  • Bot not back after reboot: re-run pm2 startup, execute the printed command, then pm2 save while the bot is running.
  • Restart loop: PM2 is restarting a crashing bot repeatedly — read pm2 logs and add --restart-delay so you can see the error.
  • High memory: cap it with --max-memory-restart to catch leaks automatically.

FAQ

Why isn't my bot restarting after a reboot?

You likely skipped pm2 startup or pm2 save. Run pm2 startup, execute the command it prints, then pm2 save while the bot is running.

Can PM2 run a Python bot too?

Yes: pm2 start bot.py --interpreter python3 --name mybot. That said, systemd is often cleaner for Python bots.

How do I see why my bot crashed?

pm2 logs discord-bot shows recent stdout/stderr. PM2 also stores logs on disk under ~/.pm2/logs.

Does PM2 use a lot of resources?

No — PM2 itself is lightweight. Your bot's own memory use matters more; cap it with --max-memory-restart to catch leaks.

Keep your bot online for good on Discord bot hosting from Nxeon — a small NVMe VPS with full root access runs PM2 and several bots with room to spare.

#discord bot#pm2#nodejs#process manager#vps#seobatch

Deploy your first server in under a minute

Creating an account is free and takes no card details. You pay when you deploy — choose a billing term and pay from your wallet or by card at checkout.