A common mistake when deploying your first app or Discord bot on a Linux VPS is running node index.js or python3 bot.py directly inside an SSH terminal. The moment you close your terminal or your internet disconnects, Linux sends SIGHUP and kills the process.
To keep applications online 24/7—and automatically bring them back up after a crash or VPS reboot—use PM2 (ideal for Node.js/JavaScript) or systemd (built into every modern Linux distribution for any language).
Option 1: PM2 (Best for Node.js, TypeScript & Discord.js)
PM2 is a production process manager that handles auto-restarts, log management, memory limits, and boot persistence.
1. Install PM2 globally
sudo npm install -g pm2
2. Start your application
Navigate to your project folder and launch your entry file:
cd /root/my-bot
pm2 start index.js --name "my-bot"
Useful flags you can pass:
--max-memory-restart 300M: Automatically restarts the app if a memory leak pushes it above 300MB.--time: Adds timestamps to every line in PM2 logs.
3. Enable Auto-Start on VPS Reboot
Run these two commands so PM2 saves your running process list and registers a startup hook:
pm2 startup
pm2 save
(If pm2 startup outputs a sudo env PATH=... command, copy and run that printed command, then run pm2 save.)
4. Daily PM2 Commands
| Action | Command |
|---|---|
| View running apps & CPU/RAM | pm2 list |
| Live log stream | pm2 logs my-bot |
| Restart after code update | pm2 restart my-bot |
| Stop / delete process | pm2 stop my-bot / pm2 delete my-bot |
Option 2: Native systemd Service (Best for Python, Go, Rust & Everything)
systemd requires zero third-party tools, starts apps in milliseconds, and integrates cleanly with journalctl logs.
1. Create a Service Unit File
Suppose you have a Python bot in /opt/my-bot/bot.py using a virtual environment /opt/my-bot/venv/bin/python:
sudo tee /etc/systemd/system/my-bot.service > /dev/null << 'EOF'
[Unit]
Description=My Discord Bot Service
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
WorkingDirectory=/opt/my-bot
ExecStart=/opt/my-bot/venv/bin/python /opt/my-bot/bot.py
Restart=always
RestartSec=5
Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target
EOF
2. Enable and Start the Service
sudo systemctl daemon-reload
sudo systemctl enable --now my-bot.service
3. Check Status & Logs
sudo systemctl status my-bot.service
sudo journalctl -u my-bot.service -f
With Restart=always and RestartSec=5, if your script crashes or hits an unhandled exception, systemd waits 5 seconds and automatically brings it back online.
Which One Should You Choose?
- Use PM2 if you run Node.js/Next.js/Discord.js apps and want quick one-line commands, built-in memory-limit restarts, and cluster mode.
- Use
systemdif you run Python, Go, Java, or Rust services, or want zero extra dependencies on a lean Ubuntu/Debian VPS.
Frequently Asked Questions
Why did my bot stop after pm2 save when the VPS rebooted?
You likely skipped running pm2 startup before pm2 save, or you forgot to execute the sudo env PATH=... line that pm2 startup prints.
Can I run multiple bots on a single VPS-XS (2GB DDR5)?
Yes. A typical Node.js or Python Discord bot uses only 50MB–150MB of RAM, so a 2GB DDR5 Ryzen VPS can comfortably host multiple bots, databases, and small web APIs simultaneously.