Screen

Intro

Screen is a tool you can use to keep a session open on a server while you’re not logged in, this allows you to run long running jobs in the background without the fear of it exiting once you do eventually log off, are kicked off due to a network issue, etc.

Running/Detaching a Screen Session

To open a screen session all you have to is type ‘screen’ on the command line, this will open a new interactive session.

$ screen

All you need to do now is run your job and once that’s done you need to detach the screen, this will cause it to run in the background uninterrupted, to do this all you need to do is press ctrl+a d – (which is read ctrl and the letter ‘a’ together, and d after letting go of ctrl and ‘a’). This will lead you back into your previous non-screen session.  Now you can exit the server and the screen session will still be running.

Managing a Screen Session

Now to manage your screen session all you need to is type the following ‘screen -ls’

$ screen -ls
There is a screen on:
2431.pts-15.host (Detached)
1 Socket in /var/run/screen/S-user.

This will list all the current Screen sessions you have opened, to reattach on to the session again, all you need to do is type ‘screen -r’ if there is one screen session open or you can be more specific (helpful when you have multiple screen sessions open) ‘screen -r 2431’

$ screen -ls
There are screens on:
3273.pts-15.host (Detached)
2431.pts-15.host (Detached)
2 Sockets in /var/run/screen/S-user.

$ screen -r 2431

Killing a Screen Session

To kill a screen session you can either exit from the screen session itself or type the following ‘kill 2431’ where 2431 is the process number of the screen session

$ screen -ls
There are screens on:
3273.pts-15.host (Detached)
2431.pts-15.host (Detached)
2 Sockets in /var/run/screen/S-user.

$ kill 2431
$ screen -ls
There is a screen on:
3273.pts-15.host (Detached)
1 Socket in /var/run/screen/S-user.

Disclaimer

Always make sure to kill screen sessions if they’re not in use or it affects the server’s performance, and do not run this on the jump servers.