January 3, 2014

Unix command: screen

Consider this scenario: you’re connected to a remote Unix-based server (like Solaris or Linux); you invoke a program; the process starts running; while the process is still running and has not yet completed its execution, you lose the network; the process dies! The program you invoked could be anything like an installer which is just halfway through. How do you make sure you do not end up in b-e-a-utiful situations like this?

One way is to use nohup command that detaches the process from the terminal and makes the process ignore the HUP/SIGHUP signal (which is sent when the terminal session closes and the process usually exits as part of signal handling). This explains the origin of the command name too: no HUP ~ no hup ~ nohup. Other ways to detach include setting shell specific process control options like disown. However, this mechanism only detaches the process i.e. once detached, it cannot be attached to a terminal. This does not also handle processes that are interactive i.e. which expect input from the user where the ability to pause/resume or plug back into the session is required.

screen is a wonderful command that addresses all the aforementioned shortfalls and provides many more new features in the context of remote session management capabilities. It can be used to attach & detach sessions, share a session with another user, split the window horizontally/vertically and so on. You can create as many sessions as you want and manage as per your wish.

What the command essentially does underneath is that the process is detached from the connected terminal and rather managed by the screen process. Note that screen may not be available by default on your Unix system and may need to be installed explicitly.

Links

Good tutorial: http://www.rackaid.com/resources/linux-screen-tutorial-and-how-to/
Short introduction: http://www.pixelbeat.org/lkdb/screen.html
Official Website: http://www.gnu.org/software/screen/

No comments: