MonitoringUptime3 min read

Monitor your bot uptime

Set up alerts to know if your bot disconnects and restart it automatically.


Why monitor your bot

Discord bots can go offline due to token issues, rate limits, memory leaks, or network problems. Proper monitoring ensures you know immediately when something goes wrong.

Method 1: systemd auto-restart

If you're using systemd, configure aggressive restart policies:

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

[Service]
User=deploy
WorkingDirectory=/opt/bots/my-bot
ExecStart=/opt/bots/my-bot/venv/bin/python run.py
Restart=always
RestartSec=5
StartLimitIntervalSec=60
StartLimitBurst=5

[Install]
WantedBy=multi-user.target

This restarts the bot within 5 seconds of a crash, up to 5 times per minute.

bash
sudo systemctl daemon-reload
sudo systemctl restart discord-bot

Method 2: PM2 monitoring (Node.js)

PM2 provides built-in monitoring and restart capabilities:

bash
# View real-time metrics
pm2 monit

# Set max memory (auto-restart if exceeded)
pm2 start bot.js --max-memory-restart 300M

# Set max restarts
pm2 start bot.js --max-restarts 10

Method 3: Health check script

Create a script that checks if your bot is responsive and alerts you:

bash
nano /opt/scripts/bot-health.sh
bash
#!/bin/bash
BOT_SERVICE="discord-bot"
WEBHOOK_URL="https://discord.com/api/webhooks/YOUR_WEBHOOK_ID/YOUR_WEBHOOK_TOKEN"
LOG_FILE="/var/log/bot-health.log"

# Check if service is running
if ! systemctl is-active --quiet $BOT_SERVICE; then
  TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
  MESSAGE="⚠️ **Bot Down!** Service \`$BOT_SERVICE\` is not running on \`$(hostname)\` at $TIMESTAMP. Attempting restart..."
  
  # Send Discord webhook alert
  curl -s -H "Content-Type: application/json" \
    -d "{\"content\": \"$MESSAGE\"}" \
    $WEBHOOK_URL
  
  # Attempt restart
  sudo systemctl restart $BOT_SERVICE
  
  # Log the event
  echo "[$TIMESTAMP] Bot was down. Restart attempted." >> $LOG_FILE
  
  # Check if restart was successful
  sleep 5
  if systemctl is-active --quiet $BOT_SERVICE; then
    curl -s -H "Content-Type: application/json" \
      -d "{\"content\": \"✅ Bot successfully restarted.\"}" \
      $WEBHOOK_URL
  else
    curl -s -H "Content-Type: application/json" \
      -d "{\"content\": \"❌ Bot restart FAILED. Manual intervention required.\"}" \
      $WEBHOOK_URL
  fi
fi

Make executable and schedule:

bash
chmod +x /opt/scripts/bot-health.sh
crontab -e
terminal
*/2 * * * * /opt/scripts/bot-health.sh

This checks every 2 minutes.

Method 4: Discord webhook alerts from your bot

Add a heartbeat to your bot code that sends periodic status updates:

Node.js example

javascript
// Send heartbeat every 5 minutes
setInterval(async () => {
  const uptime = process.uptime();
  const memory = process.memoryUsage();
  console.log(`Heartbeat: uptime=${Math.floor(uptime/60)}min, rss=${Math.floor(memory.rss/1024/1024)}MB`);
}, 5 * 60 * 1000);

Python example

python
import asyncio
import psutil

@tasks.loop(minutes=5)
async def heartbeat():
    process = psutil.Process()
    memory = process.memory_info().rss / 1024 / 1024
    print(f"Heartbeat: memory={memory:.1f}MB, guilds={len(bot.guilds)}")

Monitoring dashboard

For a visual overview, check your bot's status in the Baires Host panel:

  1. Go to Services → select your bot hosting plan
  2. View CPU, RAM, and network usage graphs
  3. Check uptime percentage over the last 30 days

Best practices

  • Set up alerts for both downtime AND high resource usage
  • Keep restart logs to identify patterns (time of day, memory growth)
  • Monitor Discord API rate limits in your bot logs
  • Use a separate monitoring webhook channel to avoid spam in main channels

Was this guide helpful?