Scripting GNU Screen with Python

May 21, 2011

GNU Screen is one sweet linux program that has boosted my productivity a lot. You can read about some of its features here. As a developer who spends most of his time in the terminal, GNU Screen is a godsend. Notably, GNU Screen sessions persist even after you've lost a connection, so losing a work in progress before a save is one less thing to worry about, and you can also split the terminal screen into numerous windows using GNU Screen.

I find splitting the terminal screen useful because I can check the output of multiple programs simultaneously. Setting up a split screen and running the desired programs is a matter of typing a few commands, but I'm very lazy, so I wrote a python script to automate it and get rid of the tedium of repeating these same commands.

Run it like this for example:

./splitscr.py -s "top" "tail -f /var/log/syslog"

This will start a new screen session and split the window to run the two commands. Use the "-s" command line argument to create a new screen session. This is useful even if you have a screen session already running because GNU Screen doesn't save your split configuration when you detach from a session. Creating a new screen session in your current screen session is a way around this. Just remember that to issue commands to the screen within the screen you need to use Ctrl-a a (ie Ctrl-a a Tab to navigate between windows). The extra "a" sends the command to the nested screen.

Further Reading:

http://www.jerri.de/blog/archives/2006/05/02/scripting_screen_for_fun_and_profit/
http://blog.lathi.net/articles/2008/09/13/scripting-screen