Unix screen command – Use cases

screen command

Unix screen command offers lot of features that we can make use of. In this article, we can see two use cases that we come across more frequently where we can make use of screen to make things simple.

Scenario

Consider the below list of scenario.

  1. You have got a request to run a long running batch job 30 mins before your work hour ends. The job cannot be interrupted once you initiate it in terminal . You need to leave office in 30 mins and you never know when the job will finish once you initiate it. If you lose connection the job gets interrupted.
  2. You are conducting a technical interview from remote through phone and you want the candidate to perform few tasks in terminal. You want to follow along with the candidate and analyze his/her skills. How can you achieve this ?

 

Screen Command

Screen command offers the ability to detach a long running process (or program, or shell-script) from a session and then attach it back at a later time. When the session is detached, the process that was originally started from the screen will still be running and managed by the screen. You can then re-attach the session at a later time, and your terminals are still there, the way you left them. One of the other feature that screen command allows is to share the session with other users.

Firstly lets see how we can achieve the requirement specified in the scenario section.

 

Solution for Scenario 1:

Typically you’ll execute a script or batch job as below.

./batch-job.sh

You need to remain in the session and without losing internet connectivity. With respect to our scenario, you need to wait along for a long time until the script/job completes.

Instead, you can start a screen session just to execute the script, detach it and leave office. You can go home, reconnect and reattach with the screen session which you created for executing the script and check the status of the job.

Lets see how we can do this.

 

Create a screen session:

Attached mode:

screen -S batch_job

The above command creates a new terminal session with the name ‘batch_job’ and makes it as your current terminal session (session gets attached). ‘-S’ denotes name of the session. You can use any name for your session.

 

Detached mode:

 screen -d -m -S batch_job

The above command creates a new terminal session with the name ‘batch_job’ in detached mode. It does not make the screen session which you created as your current terminal session. Instead you’ll still remain in your old terminal session.

 

Note:

You can create a screen session by just typing ‘screen’ from the terminal. In this case a session will be created in attached mode and the name of the session will be some random numbers.

 

List screen sessions:

screen -ls

The above command will display list of available screen sessions and their state. In our case, we will get the the output as below.

If you have used non-detached mode, you’ll get the below output

# screen -ls
There is a screen on:
27129.batch_job (01/23/17 05:08:31)     (Attached)
1 Socket in /var/run/screen/S-root.

If you have created in detached mode, you’ll get the below output.

# screen -ls
There is a screen on:
27129.batch_job    (01/23/17 05:08:32)    (Detached)
1 Socket in /var/run/screen/S-root.

 

Reattach to screen session:

In the case you have created the screen session in detached mode, you can re-attach to it to execute the batch job using the below command.

 screen -r batch_job

 

Execute the batch job in ‘batch_job’ screen session:

Once you are attached to ‘batch_job’, you can run the script to trigger the batch job

 ./batch-job.sh

 

Detach your session and leave home:

Once you run the job in the screen session, you can detach it and leave home and rest assured that the job will run smooth even if you disconnect your network. You can detach the session in following ways.

Detach method 1:

When the command is executing, press CTRL+A followed by d to detach the screen.

 

Detach method 2:

When the command is executing, You can directly close the terminal ( Do not press CTRL+C ).

 

Detach method 3:

When the command is executing, In another terminal enter the below command.

 screen -d batch_job

 

Verify the job status

Once you reach home, you can verify the status of the job by re-attaching the screen session which you created.

 screen -r batch_job

You’ll see either the job might have finished running or still be running.

 

Run it in single command

You can do all the above in a single command.

Attached mode:

screen -S batch_job ./batch-job.sh

Detached mode:

screen -d -m -S batch_job ./batch-job.sh

You can then re-attach to the screen whenever you want to check the status as discussed above.

 

Solution for Scenario 2:

Screen provides option to share your the screen with multiple users. This scenario requires a server where both you and your candidate can access. You can make use of a free tier EC2 server from AWS. You can ask the candidate to start a screen session or you can create one and ask  him/her to join ( User account of both users should be the same )

Create a screen session:

We have already discussed creating screen session in detached and attached mode. For now lets create it in attached mode.

screen -S interview

Share your session:

Ask your candidate to execute the below command or vice-versa in case you are connecting.

screen -x interview

Now both of you are sharing the same screen session and either of you can see what you or the candidate is typing.

 

Conclusion:

This article might have given you a basic idea on the usage of screen command. We have seen use cases that are common for sys admins and how screen can save our time. There are many other things that you can do with screen like locking your screen session with password, Logging the commands that we use in a file and more.  You can check the screen manual on the various options that we can use along with screen and deep dive if you want to explore more.