When you log into a Linux VPS over SSH and start a process (like node bot.js or python3 bot.py), that process is attached to your active SSH terminal session. As soon as you close your terminal or disconnect from SSH, the operating system kernel sends a SIGHUP (Signal Hangup) to all child processes, immediately terminating your bot.
In this guide, we cover four effective methods to keep your bots and scripts running continuously on your RivinCloud VPS even after logging out.
1. The 4 Methods Overview
┌─────────────────────────────────────────────────────────┐
│ How do you want to detach your process from SSH? │
└────────────────────────────┬────────────────────────────┘
│
┌──────────────────┬───────────────┴───────────────┬──────────────────┐
▼ ▼ ▼ ▼
Method 1: nohup Method 2: Screen Method 3: Tmux Method 4: PM2 (Best)
(Simple background) (Multiplexer session) (Advanced session) (Supervisor + Auto-restart)
2. Method 1: Using nohup (Simple Backgrounding)
The nohup (No Hangup) command instructs the Linux kernel to ignore SIGHUP signals.
nohup node index.js > bot.log 2>&1 &
nohup: Ignores disconnect signals.> bot.log: Redirects standard output tobot.log.2>&1: Redirects error output to the same log file.&: Runs the process in the background.
To check if it's running:
ps aux | grep node
To stop it:
kill <PID>
3. Method 2: Using GNU Screen
screen creates a virtual terminal session that remains active on the server independently of your SSH login.
- Start a named screen session:
screen -S mybot - Start your bot normally:
python3 bot.py - Detach from session: Press
Ctrl + A, then pressD. You can now safely close your SSH terminal! - Re-attach to the session later:
screen -r mybot
4. Method 3: Using PM2 (Production Standard)
For production Node.js or Python bots, PM2 is the superior solution because it not only detaches the process, but also handles auto-restarts on crash and server reboots:
# Install PM2
sudo npm install -g pm2
# Start process
pm2 start index.js --name "discord-bot"
# Save state for VPS reboots
pm2 save
Command of the Day: nohup
The nohup command allows commands to run in the background immune to terminal hangups.
Syntax
nohup [command] [arguments] [redirection] &
Practical Example
nohup python3 -u script.py > script.log 2>&1 &
Explanation
-u: Tells Python to run in unbuffered mode so log lines are written toscript.logimmediately without buffering delay.- The trailing
&releases your prompt so you can continue using the terminal.
Common Mistakes
- Forgetting the trailing
&withnohup: Without&, the command occupies your active terminal prompt. - Forgetting to save PM2 list: Running
pm2 startwithoutpm2 savemeans the process won't restore automatically if your VPS reboots.
Troubleshooting
nohup: ignoring input and appending output to 'nohup.out': You didn't specify a log file destination, sonohupcreated a defaultnohup.outfile in your current directory.
Best Practices
- Use
screenortmuxfor interactive command interfaces. - Use
PM2orSystemdfor production 24/7 background daemons.
Frequently Asked Questions
How do I kill a process started with nohup?
Find its Process ID using pgrep -f "script.py", then send a termination signal: kill <PID>.
Conclusion
Keeping bots running after SSH disconnects is fundamental to cloud server administration. Leverage nohup, screen, or PM2 on your RivinCloud VPS to ensure continuous bot operation!