Why I’m a Screen evangelist (and how to get started)

I’ve recently made several upgrades to my workflow that make me say “WHY OH WHY did it take me this long to start doing this?”  One of the big upgrades was using Sublime + Enhanced-R as explained by Alyssa Frazee to send code line-by-line to the Terminal with a simple keystroke.  Copy and paste no more!  It makes my fingers hurt just thinking about my old ways.  Thing is, I picked up on the Sublime workflow months after Alyssa and others in my department had started using it.  I read Alyssa’s blog post back in August, but it took me a while to take action.

Not this time!  Following a recommendation from Taki Shinohara, I recently started using GNU screen, or “screen” for short, and I am in nerd heaven.  I’m so excited about this that I am constantly asking everyone if they are “using screen yet?”.  I am sure I am getting on some nerves.  But seriously, it’s the awesomest.  Here’s why.

I often work on my department’s cluster interactively, whether for debugging code that I will submit as a batch job later, or just running code that doesn’t really need to be submitted as a batch job.  I love this way of working:  I get the best of the cluster and my computer at the same time. I can use tons of memory (100G in some cases) and my computer doesn’t get bogged down.  At the same time, I can debug as I go, easily track my code’s progress, and write code as I go.  However, there is one major issue that, until recently, stopped me from working this way most of the time: the connection to the cluster could be lost, taking all my code’s progress down with it.  This can happen for a myriad of reasons: a faulty internet connection, I close my computer, I need to go home for the day, the connection simply times out after a while, and so on.  To get over these limitations, I would often use interactive sessions for the purpose of debugging and code building, but submit my code as a batch job when I needed it to run without interruption.  Alternatively, I would often work interactively, but save my progress in data files periodically and reload the saved files if the connection was lost for any reason.  This workflow does the job, but it’s not convenient or failproof.

Enter screen.  Screen allows me to work interactively for an indefinite length of time without ever having to worry about losing the connection.  A quick rundown:

1. I log onto the cluster and start a new screen by typing ‘screen’.

2. I get on a cluster node (‘qrsh’), start R or MATLAB, and submit whatever code I want to run.

3. When I am ready to stop for the time being, I type ctrl-A ctrl-D to “detach” the screen.

I can then close my computer and move on with my life, and my session will still be there the next time I need it.

4. When I’m ready to look at it again, I log onto the cluster and type ‘screen -r’ to “reattach” the screen.  And it’s back, just like magic.  This works even if code is running when I want to stop working.

Screen also works if the connection is inadvertently lost.  If you close your computer or lose your internet connection before detaching the screen, you simply need to “force detach” before reattaching it.  To do this, log onto the cluster, type ‘screen -D’ to force detach and then ‘screen -r’ to reattach.

You can also have more than one screen session open at once, running completely independently.  In this case, each screen session has an ID, and to reattach screen with ID 12345, you’d type ‘screen -r 12345’.  If you don’t know the screen ID, simply type ‘screen -r’ and screen will return a list of each session and their IDs.

Keep in mind that it’s good practice to exit screen (type ‘ctrl-A ctrl-K’ to kill an attached session) when you’re done working to make sure you aren’t taking up resources that might be needed by other users.

That’s it.  Simple and super convenient.  As long as screen is installed on your cluster, you don’t have to do anything additionally to start using it and stop losing work!

11 thoughts on “Why I’m a Screen evangelist (and how to get started)

    1. Thanks for the tip! It’s nice to be able to use Ctrl+A to go to the beginning of line even when using Screen.

  1. Spencer Debenport

    I work on a cluster back at my University while I am doing fieldwork abroad, and would not survive without screen!

  2. elizabethmargaretsweeney

    Thanks for the post Mandy — I keep getting kicked out of my interactive sessions today… Using screen now and so far so good 🙂

  3. Trang Q Nguyen

    Thank you, Mandy for all the awesome details. Thanks Yenny for telling me about screen and about Mandy’s post. This is super helpful!

  4. I always assumed screen was complicated, thanks for posting this super easy intro!!

    For some reason
    cntrl-A cntrl-k
    wasn’t killing screens for me, but
    cntrl-A : quit
    works OK.

    Has the command changed? Or might there just be something weird going on in my setup? (Supposed to be out of box?)

    1. Glad you liked it! To kill an active screen, I usually just exit R, then exit the node, then type ‘exit’. Your solution is probably a bit more convenient. I thought ctrl-A ctrl-K was supposed to work also, but I could be wrong.

      1. Ahhhh! Of course, exit -> exit. Totally didn’t occur to me, I think that’s equally convenient, and easier to remember. Thanks!

  5. Just found this, and am loving it for easier tracking of multiple (named) sessions

    ctrl+a : sessionname [name]


    1. That’s great! I hate having to guess at which session is which, though I’ve gotten fairly good at it. 🙂 I’ll definitely give this a try!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: