Managing Linux terminals using Screen

      Comments Off on Managing Linux terminals using Screen

Many of us may have encountered this scenario:
You are running some terminal-based scripts on your Linux workstations in the lab/office but its taking too long to complete. You need to call it a day at the office but would like to check up on the progress of your script the moment you get home. You try to ssh into your workstation from home, but realise there is no way to access your open terminal at the workstation. Dejected, you get back to the office the next day only to discover that your script ran into an exception half-way through.

Is there a solution to this common problem ? Well, you betcha! The solution is a nifty tool called ‘screen’ which is essentially a terminal-multiplexer. It can be used to manage multiple terminals (which could potentially be on different machines) using a single terminal window. While ‘screen’ is powerful and has various features, it also provides a nice solution to the above-mentioned scenario.

Essentially, using screen, we can “detach” the process running in a terminal, letting it run in the background; head back home, and use our home pc/laptop to establish an SSH connection back to our workstation/server at the office and magically resume where we left off.

Here’s how to do it:

Step 1:
In Ubuntu, screen is not installed by default. So, to install screen, type:

 $ sudo apt-get install screen 

Step 2:
To start a screen session, simply type:

 $ screen 

Step 3:
Then, run any code or script in the terminal. While the code is running, press CTRL-A and d.
This would detach the terminal but the process would still be running in the background.
Step 4:
From the remote computer, establish an SSH connection to the workstation/server.
Once logged on, type:

 $ screen -ls 

to list all active screen sessions.
Step 5:

 $ screen -r 

to re-attach the terminal and you should be right back where you left off.

Check out this page for more information on this tool.