How to Keep Processes Running after SSH Logout in Linux

It happens many time that we try to access an app or content, but it ask for re-login or a popup which states your session is timed out. The session generally times out when content is kept idle and no transaction is performed. Many times "session_time" variable is set, which keeps active connection for time being. But what happens when session times out, a "SIGNUP" signal is sent to processes running in background as well as for processes that are children of the main process which are forced to terminate regardless of completion or partial completion of task. So how can we keep are the process running even after SSH Logout? In this article, I will explain how to keep the process running even after SSH is disconnected from a Linux terminal (Ubuntu 18.04 and CentOS 7).

1) Screen Command

The screen utility provides a way to run a command on a Linux system, detaching, and then reattach later. Screen is particularly handy if you have a time consuming process that you want to keep running even after you log out and still you have option to reattach to it later and that to from another location.

Using a single Screen session

TIP: we are using 'top' command here, you can run command in screen session. Press "ctrl+a" and "d" immediately to DETACH from screen session; It will keep running in background.
To enter into detached screen session

$ screen -r

While in screen; use 'exit' to end screen session completely.

$ exit
[screen is terminating]

You're in your parent bash shell; Check by 'screen -r' if there is any screen session

$ screen -r

There is no screen to be resumed.

Add Multiple Screen

Add more Screen sessions and simply switch from one to another.We've seen how to use single screen session above. Let's try multiple. Add first screen:

$ screen
(run 'top' from screen session; And "ctrl+a" + "d")
$ top	

[detached from 15603.pts-1.centos7]

Now, add second screen:

$ screen

Run any command from this new screen session as per your wish. Let's try 'df' to check mounted disks.(Run 'df -hT' from screen session; And "ctrl+a" + "d")

$ df -hT 

[detached from 15652.pts-1.centos7]

BONUS: Screen names too long? Let's name the session; name will replace
This will name the session as per your wish; let's name a session as 'ping'.

$ screen -S 	
$ screen -S ping

Then detach the session. You'll see session name right after pid; instead of

$ ping -c5		
[detached from]
 $ screen -r
There are several suitable screens on:      (Detached)				
        15652.pts-1.centos7     (Detached)
        15603.pts-1.centos7     (Detached)
Type "screen [-d] -r [pid.]" to resume one of them.

2) disown Command

Top command is like looking into processes tab in Windows task manager. Top command tells everything about current running, dead, zombied, etc processes. Also gives CPU usage and CPU load average as per 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:40 ? 00:00:03 nautilus-desktop --force
root 10832 10724 0 19:36 pts/2 00:00:00 top
root 10915 10724 0 19:37 pts/2 00:00:00 grep --color=auto top

3) nohup Command

Let's run yum using nohup to install a 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) setsid Command

setsid - creates a session and sets the process group ID

setsid() creates a new session if the calling process is not a process group leader.PID of the calling process are set to process session ID and respective group ID of calling process.

$ setsid iostat
$ ps -ef | grep iostat

To Terminate Command use CLT+C

5) Tmux Command

Tmux is a Terminal Multiplexer. Switch between several programs in one terminal is made easy, detach them (they keep running in the background) and reattach them to a different terminal.

Attach & Detatch tmux session

$ tmux attach
$ tmux detach

Exiting/Terminating tmux

$ exit

Naming the sessions while creating it

$ tmux new -s

Attaching by using name

$ tmux attach

Switching Between sessions using names

$ tmux switch -t

Sessions are useful for separating various work environments. I generally have a ‘Office’ session and a ‘Home’ session; in ‘Office’, I keep everything open that I need during my day-to-day development, while in ‘Home’, I keep open current open-source gems to hack on at home.

6) byobu Command

Many distro does not support bayou please follow below commands to get byobu installed on CentOS, Ubuntu or Fedora.

$ sudo apt-get install byobu (Ubuntu)
$ sudo yum install byobu (CentOS)

Once installed for your respective environment use this below simple command,

$ byobu

Now we are going to initiate the long running process and exit.

$ tar -xvzf community_images.tar.gz

Press "F6" for background execution of process.
After login to remote system, just type byobu to re-attach session.

$ byobu

Related Articles

What other way do you think of keeping the process running even after you logout from SSH session? Do mention in your comments. Thanks for following and liking our articles.

Leave a Comment