Nothing frustrates chat server communities more than a bot that repeatedly goes offline, misses commands, or disconnects every few hours.
If your Discord or Telegram bot randomly stops responding, systematically working through diagnostic log traces and process supervisors will help you pinpoint and resolve the underlying issue.
In this guide, we cover the top reasons bots go offline on a Linux VPS and how to fix them.
1. Diagnostic Step 1: Check Process Logs
Never guess why a bot went offline. Inspect your process logs immediately:
If Using PM2:
# View recent error logs only
pm2 logs discord-bot --err --lines 50
If Using Systemd:
# View service logs for the current boot
sudo journalctl -u mybot.service -n 50 --no-pager
2. Top Reasons Bots Keep Going Offline
Reason 1: Unhandled Promise Rejections / Uncaught Exceptions
- Problem: In Node.js or Python, an unhandled network error (such as a failed HTTP request to an external API) crashes the entire process.
- Fix: Wrap API calls in
try...catchblocks and add global exception handlers:process.on('unhandledRejection', (reason, promise) => { console.error('Unhandled Rejection at:', promise, 'reason:', reason); });
Reason 2: Linux Out-Of-Memory (OOM) Killer
- Problem: If your bot leaks memory over time, your VPS may run out of RAM. The Linux kernel kernel-level OOM Killer terminates the heaviest RAM process (often Node.js or Python) instantly.
- Diagnosis: Check kernel logs:
sudo dmesg -T | grep -i oom - Fix: Set memory caps in PM2 (
--max-memory-restart 250M) and inspect code for event listener leaks.
Reason 3: Discord Gateway Rate Limits (429 Too Many Requests)
- Problem: Sending commands too quickly or triggering rapid reconnect loops forces Discord to block your bot's IP address temporarily for 10–15 minutes.
- Fix: Implement command cooldowns and configure exponential backoff delays in PM2 restarts (
exp_backoff_restart_delay: 100).
Reason 4: Missing WebSocket Heartbeats (Network Socket Timeout)
- Problem: High CPU saturation on the main thread prevents the bot from sending heartbeat pings to the Gateway within the required timeframe, causing the Gateway to sever the socket.
- Fix: Offload heavy CPU calculations (like image processing) to background worker threads.
Command of the Day: pm2 logs
The pm2 logs command streams realtime or historical log output from applications managed by PM2.
Syntax
pm2 logs [app_name_or_id] [--err] [--out] [--lines N]
Practical Examples
# 1. Stream live combined stdout and stderr logs
pm2 logs discord-bot
# 2. View only error logs from the past 100 lines without streaming
pm2 logs discord-bot --err --lines 100 --nostream
Explanation
Inspecting --err logs isolates runtime stack traces from standard informational text, quickly revealing the broken file and line number causing process exits.
Common Mistakes
- Restarting the bot manually without reading logs: Overwriting console logs before reading them masks the root cause of repeated crashes.
- Hosting on unstable residential connections: Running bots on home Wi-Fi leads to frequent socket drops due to dynamic IP rotation. Always host on high-uptime cloud VPS servers.
Troubleshooting
- Bot status is 'online' in PM2 table, but offline in Discord: The process is running, but the WebSocket connection timed out or the bot token was reset. Restart the process:
pm2 restart discord-bot.
Best Practices
- Use PM2 log rotation (
pm2-logrotate) to prevent log files from growing uncontrollably. - Implement health check endpoints or uptime ping monitoring services (like Uptime Kuma) to notify you instantly if your bot disconnects.
Frequently Asked Questions
Why does my bot crash when handling certain messages?
The message handler likely assumes specific properties exist (e.g. message.guild.name) without checking if the message was sent inside a Direct Message (DM). Always validate object properties before dereferencing!
Conclusion
Systematic log inspection with pm2 logs and robust error handling will transform an unstable bot into a rock-solid 24/7 service on your RivinCloud VPS!