Keep Processes Running on SSH
After connecting to your workload via SSH (see Connect via SSH), you can use these methods to keep processes running.
SSH sessions can disconnect due to network drops, idle timeouts, or closing the terminal. If a process is tied to that session, it may stop when the connection ends. To keep a process running on a remote machine after your SSH session disconnects, use one of these reliable methods: nohup, screen, or tmux.
Using nohup (simple + most common)
The process is detached from the SSH hangup signal and continues running in the background.
Run your command and redirect logs:
nohup python3 your_script.py > output.log 2>&1 &
What this does:
-
nohupprevents the process from stopping when the SSH session closes -
output.logsaves output -
2>&1sends errors to same log -
&puts it in the background
Check if it's still running:
ps aux | grep your_script.py
Stop it later:
pkill -f your_script.py
Using screen (helps you reconnect to same session)
A screen session persists independently of SSH and can be detached and reattached without stopping the process.
If the screen utility is not installed already, it can be installed using the following command:
sudo apt update && sudo apt install screen -y
Start a screen session:
screen -S mysession
Run your script inside:
python3 your_script.py
Detach (leave it running):
Press: Ctrl + a, then d
Reattach later:
screen -r mysession
List sessions:
screen -ls
Using tmux (more modern alternative to screen)
This approach is preferred for longer interactive work, especially when multiple windows/panes are useful for monitoring logs, dashboards, and commands side-by-side.
If the tmux utility is not installed already, it can be installed using the following command:
sudo apt update && sudo apt install tmux -y
Start a tmux session:
tmux new -s mysession
Run your script:
python3 your_script.py
Detach:
Press: Ctrl + b, then d
Reattach:
tmux attach -t mysession
List sessions:
tmux ls
Recommended approach
- For simple background jobs: use
nohup - For long-running processes you may want to reconnect to: use
tmux(best) orscreen