How to Keep Remote SSH Sessions and Processes Running After Disconnection
To Keep Remote SSH Sessions and Processes Running After Disconnection
Secure Shell is a way in which a person can use remote user or remote system through terminal. When we logout of the session the services running in pseudo-terminal are forced to terminate. To over come this we use the following ways to Keep Remote SSH Sessions and Processes Running After Disconnection.
To understand Processes on Linux
Normal Process
It is a life span of a session.
Their starting session as foreground processes and end up in certain time span. Including root, this process have owner as any one of valid user.
Orphan process
This process initially had a parent, but after some time parent process unintentionally dies making init to be the parent of that process. Such processes have init as their immediate parent which waits on these processes until they die or end up.
Daemon process
These are some orphaned processes
It is a long running process which is initated only once and then detached from any controlling terminal so that they can run in background till they do not get completed.
Techniques to Keep SSH Session Running After Disconnection
1. Using screen Command
Screen command acts as a text Window Manager for Linux. It allows user to manage multiple sessions at same time. For starting your session on screen, detach it from pseudo-terminal and logout.
To install screen
root@linuxhelp:~# apt-get install screen -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
iselect screenie byobu ncurses-term
The following NEW packages will be installed:
screen
.
.
.
Processing triggers for ureadahead (0.100.0-19) ...
By typing ' screen' command you will be in new screen session. From this session you may traverse between windows, create new windows etc.,
root@linuxhelp:~# screen
After screen session started, you may run any command. Here, we are installing vlc inside the screen.
root@linuxhelp:~# apt-get install vlc* -y
.
.
.
Get:8 http://in.archive.ubuntu.com/ubuntu/ wily/universe libsidplay2v5 amd64 2.1.1-14ubuntu2 [87.6 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu/ wily-updates/universe libswscale-ffmpeg3 amd64 7:2.7.6-0ubuntu0.15.10.1 [129 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu/ wily/universe libiso9660-8 amd64 0.83-4.2 [18.9 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu/ wily/universe libvcdinfo0 amd64 0.7.24+dfsg-0.2 [90.7 kB]
Get:12 http://in.archive.ubuntu.com/ubuntu/ wily/universe libproxy-tools amd64 0.4.11-4ubuntu3~gcc5.3 [5,218 B]
.
0% [13 vlc-data 126 kB/5,317 kB 2%] 71.5 kB/s 1h 17min 56s
0% [13 vlc-data 896 kB/5,317 kB 17%]
Detaching Screen
For detaching screen from remote terminal hit “ Ctrl+a” and “ d” .
root@linuxhelp:~# screen
[detached from 4624.pts-2.linuxhelp]
To Resume Detached Screen Session
For Detaching screen session, relogin the remote terminal by entering " screen -r" , incase if one screen is opened. For opening multiple sessions run “ screen -r < pid.tty.host> ” .
root@linuxhelp:~# screen -r 4624.pts-2.linuxhelp
.
.
.
Get:12 http://in.archive.ubuntu.com/ubuntu/ wily/universe vlc amd64 2.2.1-3 [1,496 kB]
Get:13 http://in.archive.ubuntu.com/ubuntu/ wily/universe vlc-plugin-pulse all 2.2.1-3 [904 B]
Get:14 http://in.archive.ubuntu.com/ubuntu/ wily/universe phonon-backend-vlc amd64 0.8.2-1ubuntu1 [90.8 kB]
Get:15 http://in.archive.ubuntu.com/ubuntu/ wily/universe phonon4qt5-backend-vlc amd64 0.8.2-1ubuntu1 [96.3 kB]
Get:16 http://in.archive.ubuntu.com/ubuntu/ wily/universe libvo-aacenc0 amd64 0.1.3-1 [71.0 kB]
Get:17 http://in.archive.ubuntu.com/ubuntu/ wily/main ant all 1.9.6-1ubuntu1 [1,892 kB]
4% [Working] 189 kB/s 28min 28s
For more info on screen commands: https://www.linuxhelp.com/useful-screen-command-examples/
2. Using Tmux (Terminal Multiplexer)
Tmux is used for replacement of screen. This allows splitting panes horizontally or vertically between multiple windows. For installing tmux run the following command.
root@linuxhelp:~# apt-get install tmux -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
tmux
.
.
.
Processing triggers for man-db (2.7.4-1) ...
Setting up tmux (2.0-3) ...
For starting Tmux Session
After typing tmux, you will get a new session.
root@linuxhelp:~# tmux
Now run any command on the Tmux terminal. Here we are installing VLC inside the tmux terminal.
root@linuxhelp:~# apt-get install vlc* -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
.
.
.
vlc-plugin-samba vlc-plugin-sdl vlc-plugin-svg vlc-plugin-vlsub vlc-plugin-zvbi vlevel vlock
vlogger vorbis-tools vorbisgain
0 upgraded, 335 newly installed, 0 to remove and 7 not upgraded.
Need to get 323 MB/360 MB of archives.
After this operation, 828 MB of additional disk space will be used.
Get:2 http://in.archive.ubuntu.com/ubuntu/ wily/universe libaspectj-java all 1.8.5-1 [11.2 MB]
0% [2 libaspectj-java 1,089 kB/11.2 MB 10%]
[1] 0:bash* " linuxhelp" 04:19 20-May-16
Detaching Tmux Session from Terminal
Run tmux session by using “ tmux detach” or use shortcut Ctrl+b and d. For resuming , re-login to the remote machine and type “ tmux attach” .
root@linuxhelp:~# tmux
[detached (from session 0)]
After this process your current session will detach and you will come back to your terminal.
root@linuxhelp:~# tmux attach
Get:3 http://in.archive.ubuntu.com/ubuntu/ wily/universe browser-plugin-vlc amd64 2.0.6-4 [44.0 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu/ wily/main ocaml-base-nox amd64 4.01.0-4ubuntu1 [447 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu/ wily/main libtinfo-dev amd64 5.9+20150516-2ubuntu1 [77.2 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu/ wily/main libncurses5-dev amd64 5.9+20150516-2ubuntu1 [176 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu/ wily/main ocaml-compiler-libs amd64 4.01.0-4ubuntu1 [1,294 kB]
4% [7 ocaml-compiler-libs 213 kB/1,294 kB 16%] 659 kB/s 7min 50s
[1] 0:bash* " linuxhelp" 04:23 20-May-16
For more info on tmux: https://www.linuxhelp.com/access-multiple-terminals-using-tmux-terminal/
3. Using nohup command
nohup command is a long running command that is used in background. For session logout the command is detached from controlling terminal and keeps on running in background as daemon process.
Use find command to search for files with nohup. Now the find command is started running in background.
root@linuxhelp:~# nohup find / -type f > files_in_system.out 2> & 1 &
[1] 5255
Using ' fg %JOBID' you can bring it back to foreground.
root@linuxhelp:~# fg 5255
bash: fg: job has terminated
[1]+ Exit 1 nohup find / -type f > files_in_system.out 2> & 1
4. Using disown Command
Disown command is used to remove job from job list. During session disconnection the process is shielded from being killed. It will not receive any SIGHUP by the shell. To know the process ID use ping command and check whether the process is running or not. Then use disown command to keep the ssh session running.
root@linuxhelp:~# ping linuxhelp.com > pingout &
[1] 5256
root@linuxhelp:~# jobs -l
[1]+ 5256 Running ping linuxhelp.com > pingout &
root@linuxhelp:~# disown -h %1
Relogin the remove server as given below
root@linuxhelp:~# ps -ef | grep ping
root 5256 4701 0 03:11 pts/2 00:00:00 ping linuxhelp.com
root 5260 4701 0 03:14 pts/2 00:00:00 grep --color=auto ping
5. Using setsid Command
setsid allocates a new process group to the process being executed. Now Execute any command using setsid. The process will be detached from the controlling terminal after the mentioned time interval. Here, the process will detach after 30 minutes.
root@linuxhelp:~# setsid sleep 30m root@linuxhelp:~# ps -ef | grep sleep root 5262 1434 0 03:16 ? 00:00:00 sleep 30m root 5264 4701 0 03:16 pts/2 00:00:00 grep --color=auto sleep
Relogin the session to check the running process.
root@linuxhelp:~# ps -ef | grep sleep
root 5262 1434 0 03:16 ? 00:00:00 sleep 30m