PM2Multiple botsResources4 min read

Run multiple bots on a VPS

Manage multiple bots with PM2 ecosystem files, systemd units and resource allocation on a single server.


How many bots can I run?

It depends on your VPS resources. As a reference:

VPS RAMEstimated bots
1 GB2-3 lightweight bots
2 GB4-6 bots
4 GB8-12 bots
8 GB15-25 bots

Method 1: PM2 Ecosystem File

PM2 is ideal for managing multiple Node.js processes:

javascript
// ecosystem.config.js
module.exports = {
  apps: [
    {
      name: 'bot-moderation',
      script: '/opt/bots/moderation/index.js',
      max_memory_restart: '200M',
      env: { NODE_ENV: 'production' }
    },
    {
      name: 'bot-music',
      script: '/opt/bots/music/index.js',
      max_memory_restart: '300M',
      env: { NODE_ENV: 'production' }
    },
    {
      name: 'bot-tickets',
      script: '/opt/bots/tickets/index.js',
      max_memory_restart: '150M',
      env: { NODE_ENV: 'production' }
    }
  ]
};
bash
# Start all bots
pm2 start ecosystem.config.js

# View status of all
pm2 status

# Restart a specific one
pm2 restart bot-moderation

# Save config for auto-start
pm2 save
pm2 startup

Method 2: Multiple systemd services

Create a service template:

bash
sudo nano /etc/systemd/system/discord-bot@.service
ini
[Unit]
Description=Discord Bot - %i
After=network.target

[Service]
User=deploy
WorkingDirectory=/opt/bots/%i
ExecStart=/usr/bin/node /opt/bots/%i/index.js
Restart=always
RestartSec=10
EnvironmentFile=/opt/bots/%i/.env
MemoryMax=256M
CPUQuota=25%%

[Install]
WantedBy=multi-user.target

Use it for each bot:

bash
sudo systemctl enable discord-bot@moderation
sudo systemctl enable discord-bot@music
sudo systemctl enable discord-bot@tickets
sudo systemctl start discord-bot@moderation

Recommended directory structure

terminal
/opt/bots/
├── moderation/
│   ├── index.js
│   ├── .env
│   └── package.json
├── music/
│   ├── index.js
│   ├── .env
│   └── package.json
└── tickets/
    ├── index.js
    ├── .env
    └── package.json

Resource monitoring

bash
# View memory usage per bot
pm2 monit

# Or with htop filtered
htop -p $(pgrep -d',' -f 'node.*bot')

# Total bot usage
pm2 jlist | python3 -c "import sys,json; data=json.load(sys.stdin); print(sum(p['monit']['memory']/(1024*1024) for p in data), 'MB total')"

Recommendations

  • Set memory limits per bot to prevent one from affecting others
  • Use PM2 if all bots are Node.js; systemd if you mix languages
  • Monitor RAM and CPU usage regularly
  • Consider separating critical bots onto different VPS for high availability

Was this guide helpful?