Let us say you're accessing an another system remotely via SSH. You want to run a command that takes a long time to finish and then exit the SSH session but leave the command running even if you're no longer connected with the remote system. How do you do that? As you might know, once you exit from the SSH, all running jobs will be terminated. Would you start the job from beginning? You don't have to. Here I have given 4 methods to keep a command running after you log out from the ssh session. Read on.
Table of Contents
Keep A Command Running After You Log Out Of The SSH Session In Linux
There could be many ways to do this. However, I find the following methods are easy and straight forward.
Method 1 - Using screen
The screen tool, a full-screen window manager with VT100/ANSI terminal emulation, allows you to safely detach from the SSH session without exiting the remote job. It will be helpful for those who are working with multiple remote servers. Screen command comes pre-installed in most Linux operating systems. Just in case, if it is not installed, you can install it using your distribution's package manager.
On Arch Linux and derivatives:
$ sudo pacman -S screen
On RHEL, CentOS and other RPM based systems:
$ sudo yum install screen
On Debian, Ubuntu and other DEB based systems:
$ sudo apt-get install screen
On SUSE/OpenSUSE:
$ sudo zypper install screen
After installing screen on your remote systems, start the screen session:
$ screen
The screen session has been started now. Now run whatever job or task you wanted to do on your remote system.
I am going to download Ubuntu 16.04 image:
$ wget http://mirror.waia.asn.au/ubuntu-releases/xenial/ubuntu-16.04.2-desktop-amd64.iso
Ubuntu 16.04 ISO is around 1.5GB in size. It will take nearly an hour to download in a normal broadband connection. You don't have to wait that long. Just leave the download process running on the remote system and exit from the screen session by pressing “Ctrl-A” followed by “d“. You will see an output something like below.
[detached from 1365.pts-0.server]
After detaching from the screen session, you can log out from the remote system. The remote job will keep running in the server.
You can re-attach to the screen session at any time using command:
$ screen -r
You will see that the process is still running there.
If you have more than one screen sessions, you need to type the screen session ID in order to re-attach with it. To view the number of screen sessions, run:
$ screen -ls
Sample output would be:
There are screens on:There are screens on: 1382.pts-0.server (Saturday 15 July 2017 04:39:51 IST) (Detached) 1365.pts-0.server (Saturday 15 July 2017 04:38:44 IST) (Detached) 2 Sockets in /var/run/screen/S-sk.
Now, re-attach to the desired screen session, say 1382, run:
$ screen -r 1382
For more details, refer man pages.
$ man screen
Method 2 - Using tmux
The tmux is a terminal multiplexer that enables a number of terminals to be created, accessed, and controlled from a single screen. A tmux session can be detached from a screen and continue running in the background, then later reattached. Like Screen tool, you can also use tmux to detach from an SSH session without quitting the remote jobs. Tmux command comes pre-installed on most Linux operating systems. Just in case, if it is not installed, you can install it using your distribution's package manager.
On Arch Linux and derivatives:
$ sudo pacman -S tmux
On RHEL, CentOS and other RPM based systems:
$ sudo yum install tmux
On Debian, Ubuntu and other DEB based systems:
$ sudo apt-get install tmux
On SUSE/OpenSUSE:
$ sudo zypper install tmux
After installing tmux, start the tmux session using command:
$ tmux
Now, start your task or job. Then safely detach from the tmux session without exiting the remote jobs by pressing "CTRL-b" followed by "d". This will detach your tmux session but will leave you’re doing in that session running in the background. That means all remotes will be running even if you're disconnected from the session.
To list the available sessions, run:
$ tmux ls 0: 1 windows (created Sat Jul 15 16:51:35 2017) [134x33] 1: 1 windows (created Sat Jul 15 16:53:58 2017) [134x33]
You can re-attach to the tmux session using the respective session ID as shown below:
$ tmux attach -t 0
For more details, refer man pages.
$ man tmux
Method 3 - Using Reptyr
What if you forgot to start the commands in screen/tmux in the first place? No worries. Here is where Reptyr comes in help. Reptyr helps you to move running processes between ptys. We have already published a detailed guide about Reptyr. Check out the following link.
Method 4 - Using nohup
nohup, stands for No hangup, is yet another command line utility to help you run Linux commands even after you're disconnected from the SSH sessions. Since it is part of GNU coreutils, you don't have to install it. It comes pre-installed in all Linux distributions.
The usage is absolutely easy. After logging into your remote system, all you have to do is:
$ nohup <command> &
Yes, it's that simple.
Example:
$ nohup wget http://mirror.waia.asn.au/ubuntu-releases/xenial/ubuntu-16.04.2-desktop-amd64.iso &
Now, you can exit from SSH session. The remote job will keep running.
Login to the remote system and run the following to view the list of running jobs.
$ jobs -l [1]+ 1421 Running nohup wget http://mirror.waia.asn.au/ubuntu-releases/xenial/ubuntu-16.04.2-desktop-amd64.iso &
By default, the outputs will be appended to 'nohup.out'. You can, of course, change the output file.
Here is another example.
$ nohup find -size +10M > log.txt &
The above command will find the files with size bigger than than 10M and writes the output to log.txt file.
For more details, refer the man pages.
$ man nohup
And, that's all for now folks. Have anything in mind? Feel free to let me know in the comment section below. More good stuffs to come. Keep visiting!
Cheers!
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Facebook | Twitter | Google Plus | LinkedIn | RSS feeds
Have a Good day!!