Running jobs using screen

Running processes in the background using screen

You can use screen to run a process without the need to redirect stdout and stderr using screen.  Note: This is text only.  If you need information on how to run a job in the background that requires a graphical user interface or GUI, please take a look at using X2Go.

Screen allows you to create a virtual screen that allows you to disconnect and reconnect if needed.  A basic usage is given below.  More information can be found at: http://www.gnu.org/software/screen/manual/screen.html

Sometimes the best way to explain something is by example:

Scenario:  I'm in the office and wanting to run my code on one of the departmental compute servers, "perkin" that outputs it status intermittently to stdout, but the code also writes to stderr if there's any errors.  The code will take around a week to complete, but the machine I'm using is public so will need to log out.  In addition, I would like to check on it's progress while I'm at a conference in the US.

Solution: Use screen:

I ssh into perkin, i.e.


  me@shared-desktop:~ $ ssh me@perkin

  me@perkin.maths.ox.ac.uk's password:

  me@perkin:~ $

I then check to see if there's any existing screen sessions I have running.


  me@perkin:~ $ screen -list

  No Sockets found in /var/run/screen/S-me.

If there are screens available, they will be listed.  In the above case, there are none, so I create one.


  me@perkin:~ $ screen

The display shows me some copyright information and at the bottom, the instructions Press Space or Return to end.  I then hit return.

Now I'm in a screen session I can run my program.  My program is called "project1" and writes to stdout and stderr.


  me@perkin:~ $ cd project1

  me@perkin:~/project1 $ ./project1 2>/scratch/me/project1/stderr.out | tee /scratch/me/project1/stdout.out

The first line changes into the project1 directory (where the binary is located)

The second line runs the program (in the foreground) but redirects stderr to a file in scratch and stdout to both the console and a file in scratch using the tee program.

At this point, exiting the terminal would normally stop the program, however as I'm in a screen session I can press ctrl+a d to leave the program running (see quick reference below), log out of perkin and the shared desktop computer and fly to the US confident the program is still running.

When I arrive in the US, I plug my notebook computer in to the internet and issue to following commands to see how the program is getting on.


  me@my-notebook:~  $ ssh me@gate.maths.ox.ac.uk

  me@gate.maths.ox.ac.uk's password:

  

  me@gate0:~ $  ssh me@perkin

  me@perkin:~ $ screen -x

The screen I'm presented with is the one I started earlier, with the progress of the code I've been running.  Occasionally, I may be presented with a couple of screen sessions I have running, if so I'l be prompted which one to connect to, e.g. I know the latest screen is the one I need, so I follow the instructions, i.e.


  me@perkin:~ $ screen -x 

  There are several suitable screens on:	

          15933.pts-5.perkin	(04/09/13 17:44:32)	(Detached)

          13886.pts-3.perkin	(04/09/12 17:03:45)	(Detached)

  Type "screen [-d] -r [pid.]tty.host" to resume one of them.



  me@perkin:~ $ screen -r 15933

Upon opening the screen I discover there's been some errors logged and wish to see them.

In the screen session, I can create a new virtual screen by pressing ctrl+A c (see quick reference below).  In this window, I see


  me@perkin:~ $

so I type


  me@perkin:~ $ less /scratch/me/project1/stderr.out

to see what's been happening.  I could also (in this virtual window) type:


  me@perkin:~ $ tail -f /scratch/me/project1/stderr.out

to show me the errors as they get written out by my code.

If I wish to jump to the next screen (there is only one other), I press ctrl+A n (see quick reference below).

Screen Quick Reference
 
 

This is a quick reference guide to some of the useful features of screen.

Starting

start a new screen session with session name screen -S <name>
list running sessions/screens screen -ls
attach to a running session screen -r
to session with name screen -r <name>
the ultimate attach screen -dRR (Attaches to a screen session. If the session is attached elsewhere, detaches that other display. If no session exists, creates one. If multiple sessions exist, uses the first one.)
 

Escape key

All screen commands are prefixed by an escape key, by default C-a (that's Control-a, sometimes written ^a). To send a literal C-a to the programs in screen, use C-a a.

 

Getting out

detach C-a d
detach and logout (quick exit) C-a D D
exit screen C-a : quit or exit all of the programs in screen.
force-exit screen C-a C-\ (not recommended)
 

Help

See help C-a ? (lists keybindings)

Window Management

create new window C-a c
change to last-visited active window C-a C-a (commonly used to flip-flop between two windows)
change to window by number C-a <number> (only for windows 0 to 9)
change to window by number or name C-a ' <number or title>
change to next window in list C-a n or C-a <space>
change to previous window in list C-a p or C-a <backspace>
see window list C-a " (allows you to select a window to change to)
show window bar C-a w (if you don't have window bar)
kill current window C-a k
kill all windows C-a \ (not recommended)
rename current window C-a A
 

Split screen

split display horizontally C-a S
split display vertically C-a | or C-a V (for the vanilla vertical screen patch)
jump to next display region C-a tab
remove current region C-a X
remove all regions but the current one C-a Q
 

Scripting

send a command to a named session screen -S <name> -X <command>
create a new window and run ping example.com screen -S <name> -X screen ping example.com
stuff characters into the input buffer
using bash to expand a newline character
(from here)

screen -S <name> [-p <page>] -X stuff $'quit\r'
a full example

# run bash within screen

screen -AmdS bash_shell bash

# run top within that bash session

screen -S bash_shell -p 0 -X stuff $'top\r'

 

# ... some time later

 

# stuff 'q' to tell top to quit

screen -S bash_shell -X stuff 'q'

# stuff 'exit\n' to exit bash session

screen -S bash_shell -X stuff $'exit\r'
 

Misc

redraw window C-a C-l
enter copy mode C-a [ or C-a <esc> (also used for viewing scrollback buffer)
paste C-a ]
monitor window for activity C-a M
monitor window for silence C-a _
enter digraph (for producing non-ASCII characters) C-a C-v
lock (password protect) display C-a x
enter screen command C-a :

 

Please contact us with feedback and comments about this page. Last updated on 25 Mar 2022 15:43.