Tuesday, January 9, 2018

How To Keep A Process/Command Running After Disconnecting SSH Session

https://www.2daygeek.com/how-to-keep-a-process-command-running-after-disconnecting-ssh-session

We all know that the running processes or command will be terminated when disconnecting the remote SSH session. Is there any ways to keep the processes running after ending ssh session?
Yes, it is possible. We are going to discuss about this in the today session with different methods.
Why you want to keep process running? In many cases the long running process needs to be alive, otherwise you have to start the same process once again. it is time consuming process and wasting your time also. So plan and complete your work without wasting time.
One more scenario: You are in emergency situation while performing some important task on remote server and you have to disconnect remote SSH session but want’s to keep the running process alive on the machine. How will you do that?
Also you can move the long running processes to background to avoid conflict with current activity which you are perform, later you can re-attach the session whenever you need.
It can be done by using following commands.
  • screen
  • tmux
  • nohup
  • disown
  • byobu

Method 1 – Using screen Command

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Each virtual terminal provides the functions of the DEC VT100 terminal.
It provides many useful functions & keys to control the screen natively. It allows user to detach a long running process from a session and then attach it back whenever you need. This is one of the best feature which i use in screen command very often for multiple purpose.

How to Install screen command

Screen command is pre-installed by default in most of the Linux systems. If no, follow the below procedure to get installed.
For Debian/Ubuntu, use APT-GET Command or APT Command to install screen.
$ sudo apt install screen
For RHEL/CentOS, use YUM Command to install screen.
$ sudo yum install screen
For Fedora, use DNF Command to install screen.
$ sudo dnf install screen
For Arch Linux, use Pacman Command to install screen.
$ sudo pacman -S screen
For openSUSE, use Zypper Command to install screen.
$ sudo zypper in screen

How to Detach and Re-attach the sessions

kick start the screen session before initiate a long running process.
$ screen
Now, run a long running process. For demonstration purpose, we are going to move this server backup to another remote server for disaster recovery (DR) purpose.
$ rsync -avzhe ssh /backup root@192.168.0.161:/backups/week-1/
It’s around 300GB so, it would take at least 24 hours and you don’t want to worry simply detach the session by hitting Ctrl+A followed by d. You will get output similar to bellow.
[detached from 4941.pts-0.server]
You have safely detached the session and you can logout the remote server.
Later whenever you want to see the progress, just login to remote system and input the following command to attach the session.
$ screen -r
If you have more than one screen session then run the following command to find the session ID. Once you have the session ID then attach the same.
$ screen -ls
There is a screen on:
 4941.pts-0.server (Tuesday 21 November 2017 06:58:14  IST) (Detached)
1 Socket in /run/screen/S-magi.

Method 2 – Using tmux Command

tmux stands for terminal multiplexer, it allow users to create/enable multiple terminals (vertical & horizontal) in single window, this can be accessed and controlled easily from single window when you are working with different issues.
It uses a client-server model, which allows you to share sessions between users, also you can attach terminals to a tmux session back.
Many of us says it’s similar to screen but i’m not since this offers wide range of configuration options.
Make a note: Ctrl+b is the default prefix in tmux so, to perform any action in tumx, you have to type the prefix first then required options.

How to Install tmux command

tmux command is pre-installed by default in most of the Linux systems. If no, follow the below procedure to get installed.
For Debian/Ubuntu, use APT-GET Command or APT Command to install tmux.
$ sudo apt install tmux
For RHEL/CentOS, use YUM Command to install tmux.
$ sudo yum install tmux
For Fedora, use DNF Command to install tmux.
$ sudo dnf install tmux
For Arch Linux, use Pacman Command to install tmux.
$ sudo pacman -S tmux
For openSUSE, use Zypper Command to install tmux.
$ sudo zypper in tmux

How to Detach and Reattach the session

kick start the tmux session before initiate a long running process.
$ tmux
Now, run a long running process and press Ctrl+b followed by d to detach your tmux session safely by leaving the running process.
$ rsync -avzhe ssh /backup root@192.168.0.161:/backups/week-2/
You will get similar output like below after detached tmux session.
[detached (from session 0)]
After login to remote server, run the following command to list available tmux sessions.
$ tmux ls
0: 1 windows (created Tue Nov 21 07:02:33 2017) [204x53]
Now, re-attach the tmux session using an appropriate session ID as follow.
$ tmux attach -t 0

Method 3 – Using byobu Command

byobu is a script that launches a text based window manager either screen or tmux. It allows you to enable multiple tabbed terminal sessions in the same window. Also you can detach and reattach the sessions later while your programs continue to run in the background.
The notable feature is status bar which displays two lines at the bottom of your screen. The upper one is the Caption line, and the lower one is the Hard Status.
  • Caption line : It shows your window name, user name, host name, IP address, and Menu
  • Hard Status line : It displays colored output which included distribution name & version, statistics, and information about the local system. Each status notification item is independently configurable, that can be toggled on or off by the configuration utility.

How to Install byobu command

byobu is not available in most of the distribution default repository. so, just follow the below procedure to get installed.
For Debian/Ubuntu, use APT-GET Command or APT Command to install byobu.
$ sudo apt install byobu
For RHEL/CentOS systems, enable EPEL Repository then use YUM Command to install byobu.
$ sudo yum install byobu
For Fedora, use DNF Command to install byobu.
$ sudo dnf install byobu
For Arch Linux systems, use yaourt Command or packer Command to install byobu since it’s available in AUR repository.
$ yaourt -S byobu
or
$ packer -S byobu
For openSUSE, use Zypper Command to install byobu.
For openSUSE_Leap_42.3
$ sudo zypper addrepo https://download.opensuse.org/repositories/utilities/openSUSE_Leap_42.3/utilities.repo
$ sudo zypper refresh
$ sudo zypper install byobu
For openSUSE_Leap_42.2
$ sudo zypper addrepo https://download.opensuse.org/repositories/utilities/openSUSE_Leap_42.2/utilities.repo
$ sudo zypper refresh
$ sudo zypper install byobu

How to Use This Feature

You can start the byobu session before initiate a long running process by running following command.
$ byobu
We are going to initiate the below long running process and exit from the remote server.
$ ping 2daygeek.com &
Just press F6 (detach) to leave byobu and keep it running your process in background.
[detached (from session 1)]
After login to remote system, just type byobu to re-attach your session back.
$ byobu

Method 4 – Using nohup Command

nohup stands for No Hang UP, it’s allow us to keep the process is running after disconnecting the ssh session. It comes pre-installed in all Linux distributions since it’s part of GNU coreutils.
Common Syntax
nohup Command [Arg] &
We are going to initiate the below long running process and exit from the remote server.
$ nohup ping 2daygeek.com &
[1] 10733
nohup: ignoring input and appending output to 'nohup.out'
Login to remote server and run the following command to view the background jobs.
$ jobs -l
[1]+ 10733 Running                 nohup ping 2daygeek.com &

Method 5 – Using disown Command

disown is yet another utility, it’s a bash shell feature. Its basically a shell builtin command similar to cd, pwd etc. The same has been validated through type disown command.
All the above commands which we discussed is not helpful/useful if you already running the processes. This is where disown command comes handy.
disown command deals with job table so get the job ID and use the disown command to keep the process running after disconnecting your session.
We are going to run the following job in background.
$ ping magesh.co.in > /dev/null &
[1] 13310
Run the following command to list the background jobs from job table.
$ jobs -l
[1]+ 13310 Running                 ping magesh.co.in > /dev/null &
Run the disown command followed by job ID, to prevent it from getting killed when exit the session.
$ disown -h %1
You can re-check the running process after login to remote session by running following command.
$ ps -aux | grep ping
magi     13310  0.0  0.1  31696  2376 ?        S    07:24   0:00 ping magesh.co.in
magi     13960  0.0  0.0  14240  1084 pts/3    S+   07:36   0:00 grep --color=auto ping

No comments:

Post a Comment