Secure Shell (SSH) is a common way for people to connect to remote Linux systems and run commands and applications on them. SSH creates a pseudo terminal and attaches it to your login session. When you end your session, the SSH terminal is also closed and all the processes running on it are terminated, unless you have configured them to keep running, even after session disconnection. In this article, we will learn how to keep SSH session alive after disconnect in Linux.
How to Keep SSH Session Alive After Disconnect in Linux
There are several ways to keep SSH Session alive after disconnection. We will look at a couple of simple ways to do this using screen and nohup commands.
1. Using Screen
Screen is a text Window Manager for Linux that allows you to manage multiple sessions at a time. It allows you to switch sessions, log sessions and even resume sessions. You can easily start screen sessions and detach them whereby they continue to run in the background. You can even resume the session at any time.
You can easily start screen session by typing screen command on terminal.
$ screen
This will start a new screen session, where you can create new windows, manage multiple windows, lock windows and run commands & scripts.
If you want to log out of a session but want the screen to keep running, press Ctrl+A keys and enter ‘d’. This will keep the screen running but detach you and send you back to your original terminal. In this case, your screen will not have any controlling terminal and its processes will continue to run in the background.
[detached from 1324.pts-19.ubuntu] $
When you detach from a screen, it will display the pid.tty.host information for the screen. It is used to identify the specific screen and will be required to re-login to it, so please note this information. In the above example, please note “1324.pts-19.ubuntu” string.
In case you want to login back to the detached screen, you can run screen -r command if there is only one screen. If there are multiple screens, you need to add the pid.tty.host of detached screen, you noted earlier.
$ screen -r $ screen -r <pid.tty.host>
2. Using nohup
Nohup is one of the most commonly used commands to run processes in background and prevent them termination when you close your session.
When you close your SSH session, it sends SIGHUP signal to all processes running on the screen, to terminate them. NOHUP command instructs the process to ignore this signal and continue running even after session logout. So these processes run in the background even when session terminates. Here is a command to run the find command to search for all pdf files in root folder. We add & at the end of command to run it in background. We also add nohup at its beginning to keep it running even after session disconnection or termination.
# nohup find / -type f *.pdf 2>1 & [1] 1435 # exit
When you run the above command, it will return the [JOBID] PID of the process, which you need to track its performance when you re-login.
When you re-login, you can directly check the progress of process with PID 1435.
Sometimes people get confused between usage or & and nohup. Adding & after your command only runs it in the background but even these processes will terminate when your session terminates, unless you have added nohup command at their beginning.
In this article, we have shown how to continue processes to run in session, even after it disconnects.
Also read:
How to Limit CPU Usage Per User in Linux
How to Automate mysql_secure_installation
How to List SFTP Users Who Have System Access
How to Reset Jenkins Admin User Password
How to Check CP Progress in Linux
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.