You are here: Home Help/User Support Systems (Windows, Mac, Unix) Unix and Linux Using 'screen' for Long-running Jobs

Using 'screen' for Long-running Jobs

'screen' is a program that allows a single physical terminal window on a Unix workstation to communicate with several interactive processes (command shells, email clients, MATLAB sessions, etc.).

Terminal screenshot: Before screen

After logging into one of the Unix workstations, start up screen by typing screen at your shell prompt and pressing Enter. A splash screen showing screen's version, license, and other information will appear: press Enter or the space bar to clear this.

You'll be at another shell prompt now, and by default, there's no obvious indication that screen is running. However, if you press CTRL-A and then ?, you'll see the first of screen's help windows.

Terminal screenshot: screen Help

The default command key for screen is CTRL-A, so in order to send a command to screen, you'd type CTRL-A followed by any of the letters listed here or on the second page of help. If you need to send a literal CTRL-A character to an application running inside screen, you'd type CTRL-A followed by A.

As an example, we'll start MATLAB inside the screen session by typing matlab at the shell prompt.

Terminal screenshot: Starting MATLAB

We've already written a trivial infinite loop program that will periodically show the current date and time in the MATLAB window. The code is shown here, and when this picture was taken, we had already started the program as shown.

Terminal screenshot: MATLAB Program Running

Now we'll disconnect from our MATLAB session by pressing CTRL-A followed by D -- though we no longer see any output from MATLAB ourselves, it's still running on its own. We're now back at our original shell prompt, and can even exit the session entirely by typing exit at the shell prompt.

Terminal screenshot: screen Disconnected

After a while (about three minutes in this case), if we wish to log back in and check on the progress of our program, we log back into the workstation, and can type screen -r to reattach to our old screen session, which has been running the whole time without our intervention.

Terminal screenshot: screen Reconnecting

Upon reattaching to the old MATLAB session, we see that the program has continued as if we'd never disconnected at all. It didn't pause or stop when we detached the screen session, exited the workstation, or anything. (Shortly after reattaching, we deliberately interrupted the MATLAB program as shown here.)

Terminal screenshot: screen Reconnected

After exiting MATLAB, we exited our screen session normally, by typing exit at the shell prompt. screen told us that it was exiting with the message [screen is terminating] -- then we could log out from the workstation normally.

Filed under: ,