How to use linux 'screen'

... and avoid using multiple IDL licenses. Written by Mia Morrell & Cory Donofrio.

Lab Home -- People -- Experimental facilities -- Publications -- Experimental pictures -- Links

Here's how to be a considerate lab member and minimize your use of IDL licenses. Suppose you want to run many IDL processes. It's probably a good idea to plan out and record what computer you are going to use for each of these processes before you start running them.

Before you begin: to open a terminal on computer 'computer', open a terminal and type:

        ssh -X computer.physics.emory.edu
You probably already know this.


1) Check license usage. Ask Eric how to do this.

If you are guilty of using excess licenses (shame on you!): Either log out of your excess idl sessions, or to kill all idl processes for user 'username' on a computer type:

        pkill -9 -u username idl
You can see what processes you and others are running on your computer by typing:
        top
You'll get something like this:
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM TIME+ COMMAND     
where PID is the process ID, USER is the user running the process, NI is the nice value (which represents process priority with 0 being high priority), %CPU is the percent of the core you are using (which should be in 80s to 100 if you are running at high priority), and COMMAND is the type of process that is running. The other stuff I have no idea, google it.

And you can see how long these processes have been running by typing:

        uptime

To see just what processes a specific user is running type:

	top -u username
To kill a specific idl process, type:
        kill -9 PID
(where PID is the process ID number you get with the 'top' command)

2) Running processes: Once you have cleaned up (step 1), you can start running processes! Open a terminal on your desired computer and enter:

        screen
Screen is cool because it allows you to run multiple processes from one session, access this session from other locations, and keep this session running despite not having an open shell session. It's uncool because if you aren't careful, you can accidentally use up a ton of licenses if you don't properly log out of/kill IDL processes you don't need anymore (see step 1!).

To start a process in your session, press: CONTROL-A then CONTROL-C This will open a new window in your session. Note that on Mac a new window has been created, but there will not be a different window on your computer screen. When you cycle through your screen windows (see below) they will appear in the same window on your screen. Then launch idl and be awesome. Repeat these keystrokes for every process you want to run on your computer. This way, you can run up to 16 processes on one computer using a single IDL license!

WARNING: Do not try to use any plot windows while running IDL in screen. This might work at first, but if you detach screen (see below) then your IDL session might crash if you have opened up a plot window and/or if IDL tries to plot while screen is detached. Eric recommends using screen only for situations where you do not need to plot. (This is one reason why a lot of Eric's code contains the option /noplot, or sometimes /useplot, so that he can turn off plotting when using screen.) This should be OK because the most common use of screen is to run analysis overnight when you are not trying to look at the plots.

3) Managing your session:

To reattach a window to a session, type:

        screen -r
Screen will print a list of all the sessions you have running if you have multiple sessions you can attach to. Choose the session 'session' you want to attach to, and type:
        screen -r session
To see what screen sessions you are running you can type:
	screen -list
If it lists that you have a session but it is already "attached" then the "screen -r" option will not let you reattach. But in this case you can do:
        screen -Dr
where the D says "detatch that screen session from wherever it is attached" and then the r says "reattach it to the terminal I am using now."

Have fun using Screen!


Ben Lonial wrote a nice shell script that Eric tweaked. Ben discovered that screen can dump a screen-shot into a text file. This script does one screen-shot per 'screen' and then can analyze those text files for information. The version below writes the second-to-last line into a master file. (Usually when you're running a simulation, the very last line is a blank line with a cursor.)

#!/bin/bash
        rm ~/listrun.txt
        num=$(screen -ls|grep "tached"|wc -l)

        if [ $num -gt 1 ]
        then
                screen -ls|grep "tached"|awk '{print NR,$0}'

                echo
                echo
                echo

                echo "which session?"
                read n

                dir=$(screen -ls|grep "tached"|awk '{print $1}'|\
                        awk -v var=$n 'NR==var {print NR,$1}'|awk '{print $2}')
                echo session $dir selected
        else
                dir=$(screen -ls| grep "tached"|awk '{print $1}')
        fi

        NUMS=`screen -S $dir -Q windows | sed 's/bash//g'`
        TOT=$(screen -S $dir -Q windows|\
                grep -Eo '[^[:blank:]]+'\
                |sort| grep -c bash)

        echo "total simulations running = $TOT" > listrun.txt
        echo  >> listrun.txt

        for i in $NUMS
        do
                screen -S $dir -p$i -X hardcopydir ~
                screen -S $dir -p$i -X hardcopy
                cat hardcopy.$i | tail -2 | head -1 >>listrun.txt
        done

        rm hardcopy*

        cat listrun.txt

The fourth-to-last line that starts

cat hardcopy
is where you can write your own bit of linux to pull out the information you want from each screenshot. For example, "grep" could be useful to find a specific line, and "tail -1" to pull out the last instance of that line on each screenshot.