loading...

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

Bobby Iliev on December 03, 2019

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 da... [Read Full]
markdown guide
 

I would recommend looking at tmux as an alternative.

 

Indeed tmux has simpler keyboard shortcuts, maintains window arrangement while connecting/disconnecting sessions, and uses a unix socket to control the session so giving another user access to the session is as simple as doing chown or chmod on the socket 'file'. Letting two users share a screen session is a bit of a nightmare.

 

That definitely sounds interesting! Thanks for sharing! I will be checking tmux and possibly switch to it if it works better than screen.

 

Screen also uses sockets. You can see your sockets with screen -ls. IIRC, the sockets should be under /var/run/screen.

While tmux is thrown around a lot, I've found screen to be more stable and mature. Also it can be configured pretty extensively. Just see the customization done by GRML distribution. It runs screen on every TTY by default. See an example here.

Screen does use sockets, but AFAIK there's not an option to "attach to this socket". With tmux it's just 'tmux -S /path/to/socket'. I've found this very useful for sharing a terminal while doing remote support with family/friends. I had done this via screen previously and it was significantly more effort (and involved screen's internal permissions system: wiki.networksecuritytoolkit.org/in...)

Compare the "easy 6 step" process described on the screen wiki with the following in tmux:
$ tmux -S /tmp/shared_socket # start the session using the named socket file
$ chmod 777 /tmp/shared_socket # grant any user on the system access to the socket
$ # that's it. Now the other user can use tmux -S /tmp/shared_socket to connect with full read/write access. Want to give only read access? then give them permission 6 instead of 7. Want to limit to less than all users? Then use chown, setfacl, etc.


Both can be configured extensively and launched/controlled via scripts. Both can be set as default on a tty. I find the tmux config file to be a lot easier/straight forward.


How exactly are you defining "more stable and mature"?


While I think tmux is easier to use (the config file structure and default key bindings make more sense to me), the #1 selling point for me with tmux is that if I split the screen 6 ways and detach, when I re-connect (from any device), the screen is still split 6 ways. With screen all 6 windows are still running, but I have to re-arrange them into a 6-way split again.

 

I use tmux myself on every server I'm working with. It's a simple, yet effective tool.

 
 
 
 

I highly recommend this command, we use this on daily basis instead of 3rd party tools.

 

Just be careful because by default it opens a nohup.out file and appends all output of the process you launched into this file. It can grow a lot.

 

If you're afraid of filling your disk, just toss the output to /dev/null during the command invocation ;)

 

Thanks for sharing Loik! I’ll definitely check it out!

 

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.

 

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.🐧👍

 

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

 

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!

 

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

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

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

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.

 

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.

 
 

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

 

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.

 

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).

 

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.

 

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.

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

 

For simplicity sake, you can also just run screen to get a random session. And screen -r to reconnect to your last session.

 
 

Awesome post! I use screen heaps its super simple and a breeze to install, keep them rolling! As an alternative there's also nohup/disown which are I believe baked into most Linux flavored systems. Cheers!

 

Thanks for the great feedback! I appreciate it!

 

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.

 

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.

 
 

I've not used this one either, but I've just watched a quick introduction and it looks pretty cool! Thanks for sharing!

 

I've heard that screen is insecure (not sure why, but Red Hat no longer recommends it because of the issue), and I'd recommend trying Tmux or Byobu, being that they're safer and possibly easier to work with depending on your tastes. :)

 

That's quite interesting! Thanks for sharing, I'll do some research on the vulnerability that you've mentioned! Thank you :)

 

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

 
code of conduct - report abuse