Saturday, October 12, 2013

How to use the screen command to run a program unattended

screen is a powerful terminal session manager with many use cases. One such use case is to start a long-running Command Line Interface (CLI) program in a terminal session, detach the session while leaving the program running unattended, logout, and return to the same session later from another terminal.

Let's examine how you would use screen in a real-life situation.

Imagine the time is 15 minutes to the end of your work day. But before you can leave, you have to run a program that will run for an hour. You don't want to wait around until it finishes in order to check the output. What you want is to start the program, leave, and check the output when you get home.

Assuming you have access to the work machine from home, this is how you would do it using screen.

  1. Start a new screen session.

    From a shell, run this command:

    $ screen -S mondaySession

    Note that the existing screen is immediately reset to blank. You have a new terminal session.

    The -S parameter lets you specify a name for the session. This makes it easier for you to come back to it later.

  2. Start the hour-long program.
    $ ls -al *.sh
    -rwxr-xr-x 1 peter peter  399 Oct  6  2013
    $ ./
  3. Detach the session.

    Detaching the session does not mean you are suspending its operation. In fact, the session, including the long program, is still running in the background. Detaching a session leaves the program running unattended while you travel home.

    To detach a session, send the appropriate command keyboard shortcut to screen. While a screen session is active, screen listens constantly for keyboard shortcuts. For this specific example, hit the key sequence Control-a d. This means press the Ctrl key and the 'a' key together and release, and then press the 'd' key.

    Note: All screen keyboard shortcuts begin with the Control-a key sequence.

    The terminal session is collapsed, and disappear from your sight for now. The prior session is brought back. You will see the session status as "detached":

    $ screen -S mondaySession
  4. Logout

    At this point, you can even log out, and the session still persists.

  5. Resume the session.

    After you get home, login to the work machine, and run the following command.

    $ screen -r mondaySession

    The terminal session is brought back. Note that you have access to the session as if you have never left it before, including its history, and the output of the long program that you started earlier.

    $ ls -al *.sh
    -rwxr-xr-x 1 peter peter  399 Oct  6  2013
    $ ./
    Program ended successfully. 

No discussion nowadays about screen is complete without the following disclaimer. tmux is a newer command that does similar things as screen. You can achieve the same effect described in this blog post using tmux. For now, however, you are more likely to find screen installed on a Linux box than tmux. I still find it useful to know the screen command.

1 comment:

Fine More Info said...

This type of software system is mostly operated by putting in a program that acts during a similar thanks to a server on the host pc, wherever you'll connect from. It then runs associate application on the opposite machine that connects and uses authentication and secret writing to attach remotely to the target machine.