How to keep processes running after SSH logout on Linux
This happens many times when we try to access an app or content, but it asks for a re-login or a popup that says your session has expired. Typically, the session times out when content is idle and no transaction is in progress. Many times the session_time variable is set, which keeps the connection active for now. But what happens is that when the session times out, the SIGNUP signal is sent to processes running in the background, as well as to processes that are descendants of the main process, which are forced to terminate regardless of the completion or partial completion of the task. So how can we keep the process running even after exiting SSH? In this article, I will explain how to keep the process running even after SSH is disconnected from the Linux terminal (Ubuntu 18.04 and CentOS 7).
1) Screen Command
The screen utility lets you run a command on a Linux system, detach it, and then reattach it. The screen is especially handy if you have a time-consuming process that you want it to keep running even after you log out, and you have the option to attach to it later, and that from a different location.
Using a single screen session
Tip You are using the “top” command here, you can run the command in a screen session. Press “ctrl + a” and “d” immediately to DISCONNECT from the screen session; it will continue to run in the background.
To enter a detached screen session
$ screen -r
While in the screen; use “exit” to completely end the screen session.
$ exit [screen is terminating]
You are in your parent Bash shell; Checking ‘screen -r’ if there is a session screen
$ screen -r
There is no screen to resume.
Add multiple screens
Add more screen sessions and just switch from one to the other. We saw how to use one screen session above. Let’s try a few. Add first screen:
$ screen (run 'top' from screen session; And "ctrl+a" + "d") $ top [detached from 15603.pts-1.centos7]
Now add a second screen:
$ screen
Run any command from this new screen session as you wish. Let’s try “DF” to check the mounted drives. (run “df -hT” with the session screen, and “ctrl + a” + “d”)
$ df -hT [detached from 15652.pts-1.centos7]
Bonus: are screen names too long? Let’s call the session; the name will replace tty.host. This is the name of the session as you wish; let’s call the sessions “ping”.
$ screen -S $ screen -S ping
Then detach the session. You will see the session name right after the pid; instead of tty.host
$ ping -c5 andreyex.ru [detached from 12529.ping]
$ screen -r There are several suitable screens on: 12456.ping (Detached) 12142.pts-1.centos7 (Detached) 12046.pts-1.centos7 (Detached) Type "screen [-d] -r [pid.]tty.host" to resume one of them.
2) The disown command
The Top command is similar to viewing the Processes tab in Windows Task Manager. The Top team tells everything about current, dead, etc. processes. Also shows cpu load and cpu load on average for 1 min, 5 min, 15 min timestamp.
$ top > sys_summary & $ jobs -l
[1]+ 10832 Stopped (signal) top > sys_summary
$ disown -h %1 $ ps -ef | grep top
root 2416 1979 0 13:21 ? 00:00:03 nautilus-desktop --force root 10832 10724 0 13:21 pts/2 00:00:00 top root 10915 10724 0 13:21 pts/2 00:00:00 grep --color=auto top
3) nohup command
Let’s run yum with nohup to install the package
$ nohup yum install -y httpd* > ApacheInstall 2>&1 &
Now let’s see, jobs running in the background
$ jobs -l [1]+ 3646 Running nohup yum install -y httpd* > ApacheInstall 2>&1 &
4) The setsid command
setsid – creates a session and sets a process group id
setsid () creates a new session if the calling process is not a bulk boot process. The PID of the calling process is set to handle the session ID and the corresponding group ID of the calling process.
$ setsid iostat $ ps -ef | grep iostat
To stop use CLT + C command
5) Tmux command
Tmux is a terminal multiplexer. Switching between multiple programs in one terminal is made easy by detaching them (they continue to run in the background) and reattaching them to another terminal.
Attach and detach tmux sessions
$ tmux attach $ tmux detach
Exit / exit tmux
$ exit
Session naming when creating it
$ tmux new -s
Pinning by name
$ tmux attach
Switching between sessions using names
$ tmux switch -t
Sessions can be used to separate different work environments. I usually have an “Office” session and a “Home” session; in the “cabinet”, I keep everything open that I need during my development from day to day, while in “Home”, I continue to open current open source gems for hacking at home.
6) byobu command
Many distributions do not support Bayou, please follow below commands to get byobu and install on CentOS, Ubuntu or Fedora.
$ sudo apt-get install byobu (Ubuntu)
$ sudo yum install byobu (CentOS)
Once installed for the appropriate environment, use the simple command below
$ byobu
Now we’re going to start the lengthy process and exit.
$ tar -xvzf community_images.tar.gz
Press “F6” for the background of the process execution. After logging into the remote system, simply re-enter Bayou to reattach the session.
$ byobu
How do you think how to keep the process even after logging out of the SSH session? Mention in your comments.