/ linux

SSH keep task running when disconnect

Just wanted to write down a neat thing I learned today. It's a quick little program in Linux that I didn't know the use of but seems to be installed by default on most distributions.

The scenario

So, let's say we want to do something that will take some time, liiiike for example compiling a linux kernal.

And since we're not running linux on our own machines we are SSHing to something else.

The problem

So this is what we get
Program running in SSH

And now I want to disconnect from the server, do some other stuff and then come back. BUT as soon as I disconnect, the running script will stop...

The fix - Screen

To fix this we use the brilliant and simple screen command. This will create for us a new session that runs individually from our SSH session and run the supplied command.
screen -md [my command]

Create a new screen session

This will leave us in the shell and the the new session is running in the background. We can now disconnect from the SSH session and do whatever we want.
To later reconnect we first list the running screens with screen -list

Show running screens

And then connect using screen -r [ID]

Connect to running screen

And we're back!

One thing you might notice is that when you get back you can't find the screen you started earlier. I know two reasons for this.

  • The command you started is complete and the screen session has therefore closed
  • The command has crashed, the screen is also therefore closed