DEV Community

Bobby
Bobby

Posted on • Edited on • Originally published at devdojo.com

How to keep a process running even after closing SSH connection?

There are many reasons why you would like to keep a process running even if you close your SSH session.

Here are a few examples:

  • Your working day is going to be over soon and you are running a huge database import that's taking a long time to complete, you would not want to stay and wait for the import to complete but just hand over the task to the next person on shift
  • You are downloading a huge file and it's going to take a good few hours, you would not want to leave your terminal open and wait, or even worse, you don't want to start all over again in case that your internet connection drops

What I usually do in such cases is to use the screen command and run the processes in a screen session. That way I could detach from the screen session and close my SSH connection and the process would still run allowing me or other people to attach to the session later on and follow up on the process.

Here's a quick introduction to how to use screen!

According to the official documentation screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. When screen is called, it creates a single window with a shell in it where you could run commands as normal.

I've noticed that in most cases screen is installed by default, so to check if you have screen installed you could run the following:

screen --version
Enter fullscreen mode Exit fullscreen mode

If you get command not found, then you could install screen by running:

  • For Ubuntu and Debian:
sudo apt update -y
sudo apt install screen
Enter fullscreen mode Exit fullscreen mode
  • On CentOS:
yum update -y
sudo yum install screen
Enter fullscreen mode Exit fullscreen mode

Once you have screen installed, to start a new screen session run:

screen -S SOME_NAME_HERE
Enter fullscreen mode Exit fullscreen mode

This would spin up a new screen and you would be attached to it automatically, inside the screen session run your script.

After that to detach from the screen session press CTRL+a+d.

If you need to attach back to the screen session run and check on your process just run:

screen -R SOME_NAME_HERE
Enter fullscreen mode Exit fullscreen mode

If you've forgotten the name of your screen session or if you've not set a name, you could list all available screen sessions by running:

screen -ls
Enter fullscreen mode Exit fullscreen mode

This is just a really brief introduction on how to use screen, I would recommend checking out the official documentation as well:

https://www.gnu.org/software/screen/manual/screen.html

If you're into DevOps, you’ll love DevOps Daily: it has free bite-sized exercises, tools, and guides to level up fast.

Hope that this helps!

Latest comments (44)

Collapse
 
deepasharma12345 profile image
deepasharma12345

I am working on tmux but the tmux session automatically disconnects when ssh drops unexpectedly.
Is is normal behviour of tmux?

Collapse
 
nexus_software_systems profile image
Nexus Software Systems

Screen is one way to keep a process running even after closing SSH connection. Like most things in Linux there are more ways to keep a process running even after closing SSH connection:

“Ctrl+a” immediately followed by “d” and you will be back to the terminal seeing the message that the Screen is detached. Now you can safely logout and your session will be left alive.

Tmux is another piece of software which is created to be a replacement for screen. It has most of the capabilities of screen, with few additional capabilities which make it more powerful than screen.

$ tmux detach
$ tmux attach

Executing command using nohup in background

Disown, removes the job from the process job list of the system, so the process is shielded from being killed during session disconnection as it won’t receive SIGHUP by the shell when you logout.

You can also use execute any command using setsid.
setsid allocates a new process group to the process being executed and hence, the process created is a newly allocated process group and can execute safely without fear of being killed even after session logout.🐧👍

Collapse
 
jnareb profile image
Jakub Narębski • Edited

Screen or tmux is very helpful tool if you use either some text-based tool (like e.g. text-based mail reader like alpine or mutt), or use an interactive session like e.g. ipython, and you are on not entirely reliable SSH connection. Loosing email you have been composing for 15 minutes because network connection dropped is... irritating...

For example screen -S alpine -d -RR alpine reconnect to session (forcefully closing it if it did not timed out yet), or start alpine from start if there is no screen session to go back to - for example if the host was rebooted.

The nohup command is better for batch jobs, like large wget / curl download.

Collapse
 
setzke profile image
setzke • Edited

I do:

screen -d -m $script
multitail $log1 -I $log2 -I $log3 -I $log4

My script writes everything to those logs as it works, and I get what essentially would be the output of the script as it's working. That way any CTRL+C or disconnect just quits the viewing, and not my script.

Collapse
 
bobbyiliev profile image
Bobby

That's super cool! Thanks for sharing!

Collapse
 
johan22981954 profile image
Putra Kampungan

I use Tmux. Easier than screen and more secure

Collapse
 
robyb profile image
Roberto Beccherle

There should not be things like a "brief introduction to screen" nowadays. Screen is for people already using it, if you have to start from scratch you should really consider tmux.

Collapse
 
bobbyiliev profile image
Bobby

Good point! But I think that as screen is still quite widely used, people should at least know that it exists and how to use it even if they don't use it on a daily basis.

Collapse
 
radulle profile image
Nikola Radulaški

Why not just: CTRL+Z -> bg -> disown? Sure, it suspends your job for a moment but in most cases that is not a problem.

Collapse
 
bayindirh profile image
Hakan Bayındır

Why not add & to the command to start detached, get the pid and disown in a single command. Your job doesn't get stopped, you don't lose anything (if you're capturing packets or doing something timing-critical).

Collapse
 
asharlohmar profile image
Lohmar ASHAR

I also use this a lot for jobs that are already started and don't want to lose because of some disconnection.
On a day-to-day basis it's a fair fight between screen, mostly for when I didn't had time to take too much care of the logging, and nohup + disown otherwise, because I like to avoid the hassle of cleaning old screen sessions.

Collapse
 
bobbyiliev profile image
Bobby

This is a valid point, but I'm afraid that if you do that the jobs that you have in your background would be stopped once you close your SSH session.

Also you would not want to interrupt a critical database import by suspending the job.

Collapse
 
radulle profile image
Nikola Radulaški

From my experience it doesn't get stopped. One more downside is that you can not own it again. It's like fire and forget kind of solution. Upside is that you do not have to have any preparation for it like in other cases, you run commands as usual.

Thread Thread
 
bobbyiliev profile image
Bobby

Ah yes, I've just tested that and indeed you are right!

Collapse
 
hariram_ch profile image
hariram manohar

nohup & sometimes don't work when user interaction is there then ctrl+z and bg % will work

Collapse
 
bobbyiliev profile image
Bobby

Thanks for the great feedback! I appreciate it!

Collapse
 
mihaylov profile image
Petar Petrov

I like tmux better because it allows me to also split the terminal.

Collapse
 
bobbyiliev profile image
Bobby

Hi Petar, you could do the same with screen actually.

If you press CTRL+a then SHIFT+S (note the capital S), this would create a second screen window like this.

Then you can press CTRL+a and then TAB to go to the second screen and after that press CTRL+a+c to create the second terminal.

But I'll definitely check xmux as well!

Collapse
 
mihaylov profile image
Petar Petrov

It feels clunkier than tmux, which can split horizontal and vertical.

But its good to know for systems that don't have tmux, thanks!

Thread Thread
 
moopet profile image
Ben Sinclair

Newer versions of screen let you split vertically and there's been a patch around for older versions for quite a few years.

Thread Thread
 
pklapperich profile image
pklapperich

Yeah, I was doing vertical splits in screen back on Ubuntu Dapper. It took a surprisingly long time for upstream screen to accept the patches, but distros have commonly included the patches for vertical splits since before I first used screen.