Toolserver:Screen

Problem: when you log off, the programs that were running under your session quit.

Solution: run the programs under screen.

Quick-start
Log in to a terminal on one of the login servers.

Type $ screen at a terminal. You will get a new shell prompt. Now run your program here. When you want to log off, type  . You will see: [screen detached] and you're back at the old shell prompt. Now log off; your programs will continue running in screen's virtual terminal. When you want to get them back, log in again and run $ screen -r This will resume a detached screen. To terminate a screen, resume it with   and then press  , or simply exit the shell, and screen will exit automatically.

If you are running multiple sessions,  will end the session.

If you don't want to accidentally close something important, you may want to use  instead. is not specific to screen, it's specific more to tty. It won't work in other programs like vi.

If you run many screen sessions simultaneously, it can be helpful to name them. $ screen -S operation1 To resume such a session: $ screen -r operation1

Other features

 * Opening multiple windows:Type CTRL+A c. A new screen will open.  You can switch between screens with CTRL+A #, where # is the screen to switch to (starting from 0).  All screens under the same session are detached and reattached together.
 * List all open windows:Type CTRL+A w.
 * Start a screen detached:Use  and the screen will start with your command running in it, but will not be attached.

For a detailed overview of screen, see the manual page screen(1).

Always use screen on login
By changing your ~/.bash_profile to somewhat like you will always reattach to the same screen session (or create a new one if there's no screen running). With CTRL+C d</tt> you will detach the screen and log out. Use CTRL+d</tt> or type exit</tt> to close a screen. With the last screen closed you will log out.

Running screen from cron
If you have a program that requires to be run under screen, and you need to run it automatically from cron (e.g. so it restarts automatically when the server reboots), you need to start screen in a slightly different way.

First, create a script file (e.g. myprog.sh</tt>) to start your program under screen, which should look like this:

exec screen -D -m -S myprog python $HOME/myprog.py
 * 1) ! /bin/sh

This will start a screen session called "myprog", which runs the command "python $HOME/myprog.py". The screen session will be started in the background.

Then, add an entry like this to your crontab:

0,10,20,30,40,50 * * * * cronsub -sl myprog $HOME/myprog.sh

(On Linux, use /usr/local/bin/cronsub</tt> instead.)

The name given to cronsub</tt>, in this case "myprog", should match the name you used in the script above, and the path ($HOME/myprog.sh</tt>) should be the path to the script. (The names don't have to match, but it's less confusing if they do.)

Within 10 minutes (or sooner if you run the script manually), a new screen session will be started on a login server -- but not necessarily the server you put the cron entry on. To identify which server the screen session is on, run qstat</tt>:

nightshade% qstat | grep screen 80463 0.55500 screen    rriver       r     11/17/2010 03:48:26 longrun@willow.toolserver.org      1

This tells you that the job is running on willow</tt>, so you can log into that server and run screen -r myprog</tt> to attach to the screen session.

(For more information on cronsub</tt> and qstat</tt>, see batch job scheduling.)