Keep SSH Sessions Alive After Disconnect
When you close an SSH session or lose your connection, any processes you started inside that session are usually killed. This guide shows you how to keep those processes running in the background, even after you log out. You’ll learn several reliable methods, from the simple nohup command to using terminal multiplexers like tmux and screen.
What You’ll Need
- SSH access to a remote Linux or Unix server.
- Basic familiarity with the command line.
- For the
tmuxandscreenmethods, those tools must be installed on the server (most distributions include them by default, but you may need to install them if not).
Step-by-Step
Method 1: Using nohup (No Hang Up)
- Connect to your server via SSH.
- Navigate to the directory where your process or script is located.
- Run your command prefixed with
nohupand append an ampersand (&) to send it to the background. Redirect output to a file to avoid filling the terminal. Example:nohup ./my_script.sh > output.log 2>&1 & - Press
Enterto return to the command prompt. The process is now running detached from your terminal. - You can now close the SSH session. The process will continue running.
- To check on it later, reconnect and look at the output file or use
ps aux | grep my_scriptto see if it’s still running.
Method 2: Using disown (After Starting a Process)
- Connect via SSH and start a long-running command normally, for example
./long_task. - While the process is running, press
Ctrl+Zto suspend it. - Type
bgto resume the process in the background. - Now type
disownto remove the process from the shell’s job table. This prevents the shell from sending a hang-up signal when you log out. - Close the SSH session. The process will keep running.
Method 3: Using tmux (Terminal Multiplexer)
- Connect via SSH and install tmux if needed:
sudo apt install tmux(Debian/Ubuntu) orsudo yum install tmux(RHEL/CentOS). - Start a new tmux session:
tmux new -s my_session. - Run your command inside the tmux session, e.g.,
./my_script.sh. - Detach from the session by pressing
Ctrl+bthend. You’ll return to your normal shell. - Close the SSH session. The tmux session and the process inside it continue running.
- To reconnect later, SSH back in and run
tmux attach -t my_session.
Method 4: Using screen (Alternative Multiplexer)
- Connect via SSH and install screen if needed:
sudo apt install screenorsudo yum install screen. - Start a new screen session:
screen -S my_session. - Run your command inside the screen session.
- Detach by pressing
Ctrl+athend. - Close the SSH session. The screen session remains active.
- Reattach later with
screen -r my_session.
Common Mistakes
- Forgetting to redirect output: If you use
nohupwithout redirecting output, the process may try to write to the terminal and fail, or the output is lost. Always redirect to a file. - Using
disownwithout backgrounding: You must first suspend and background the process withCtrl+Zandbgbefore disowning it. Otherwise,disownwon’t work as expected. - Not using a unique session name: If you start multiple tmux or screen sessions without names, it’s hard to tell which is which. Always use
-sor-Swith a descriptive name. - Closing the terminal before detaching: If you close the SSH session while still attached to a tmux or screen session, the session may be killed. Always detach first.
Tips
- Use
nohupfor simple, one-off commands that don’t need interaction. - Use
tmuxorscreenfor complex tasks, interactive programs, or when you need to reattach and see the output. - Combine
nohupwithniceto lower the process priority if it’s resource-intensive:nohup nice -n 10 ./script.sh & - For critical processes, consider using a process manager like
systemdorsupervisorto automatically restart them if they crash. - Always test your method with a simple command (like
sleep 60) before relying on it for important jobs.
Troubleshooting
- Process still dies after using nohup: Ensure you used
&and redirected all output. Also, check if the shell is sending SIGHUP despite nohup; try usingsetsidas well:setsid nohup ./script.sh & - Can’t reattach to tmux/screen: List active sessions with
tmux lsorscreen -ls. If the session is not listed, it may have been killed. If it’s listed as ‘dead’, you can clean it up withtmux kill-session -t name. - Process runs but produces no output: Check the output file you redirected to. If you didn’t redirect, output may be lost. Also, some programs buffer output when not attached to a terminal; use
stdbuf -oLto force line buffering. - SSH connection drops but process continues: This is normal if you used any of the methods above. If you didn’t, the process likely died. Reconnect and check with
ps.
Frequently Asked Questions
What is the difference between nohup and disown?
nohup makes a process ignore the SIGHUP signal from the start, while disown removes an already running process from the shell’s job table so it won’t receive SIGHUP when the shell exits. Both achieve a similar result, but nohup is more common for starting new background processes.
Can I keep a process running after closing SSH without any special tools?
Yes, you can use nohup or disown, which are built into most shells. However, these don’t allow you to reattach to the process later. For interactive tasks, you need a multiplexer like tmux or screen.
Will my process keep running if the server reboots?
No. None of these methods survive a system reboot. To run processes across reboots, you need to set up a service using systemd or a similar init system.
How do I see the output of a nohup process?
Since nohup redirects output to a file (usually nohup.out unless you specify otherwise), you can view it with tail -f nohup.out or open it with a text editor.
Can I use tmux and screen interchangeably?
Yes, they serve similar purposes. tmux is more modern with better scripting support, while screen is older and more widely pre-installed. Choose whichever you’re comfortable with.
What if I forget to detach from tmux and close the terminal?
If you close the terminal window (not just the SSH session) while attached, the tmux session may still survive because the SSH connection is closed gracefully and tmux detaches automatically. However, it’s safer to always detach manually.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.