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 NXClient.
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 '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 '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
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.
|start a new screen session with session name||
|list running sessions/screens||
|attach to a running session||
|to session with name||
|the ultimate attach||
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
|detach and logout (quick exit)||
|exit screen||C-a : quit or exit all of the programs in screen.|
|create new window||
|change to last-visited active window||
|change to window by number||
|change to window by number or name||
|change to next window in list||
|change to previous window in list||
|see window list||
|show window bar||
|kill current window||
|kill all windows||
|rename current window||
|split display horizontally||
|split display vertically||
|jump to next display region||
|remove current region||
|remove all regions but the current one||
|send a command to a named session||
|create a new window and run ping example.com||
|stuff characters into the input buffer
using bash to expand a newline character
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'
|enter copy mode||
|monitor window for activity||
|monitor window for silence||
|enter digraph (for producing non-ASCII characters)||
|lock (password protect) display||
|enter screen command||