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!