4 Ways To Keep A Command Running After You Log Out Of An SSH Session
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:
On RHEL, CentOS and other RPM based systems:
On Debian, Ubuntu and other DEB based systems:
On SUSE/OpenSUSE:
After installing screen on your remote systems, start the screen session:
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:
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.
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:
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:
Sample output would be:
Now, re-attach to the desired screen session, say 1382, run:
For more details, refer man pages.
Recommended Download – Free Cheat Sheet: “Linux Command Line Cheat Sheet”
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. tmux may 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:
On RHEL, CentOS and other RPM based systems:
On Debian, Ubuntu and other DEB based systems:
On SUSE/OpenSUSE:
After installing tmux, start the tmux session using command:
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:
You can re-attach to the tmux session using the respective session ID as shown below:
For more details, refer man pages.
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:
Yes, it’s that simple.
Example:
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.
By default, the outputs will be appended to ‘nohup.out’. You can, of course, change the output file.
Here is another example.
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.
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!
No comments:
Post a Comment