Sunday, November 12, 2006

Use "screen" to run programs in a dedicated session

I'm so surprised that no many people know about this command. Suppose I run a long term command such as 'top' in my notebook, with a PuTTY session. Suddenly the notebook out of battery and standby automatically. After I reopen the notebook the session has already close.

It would not be a problem for this case, but imagine that I am running a backup job or evening some billing job and the session terminated unexpectedly. It would be a nightmare. Fortunately, using 'screen' command may make our life easier.

So when you need to run a command, don't run "top" but "screen top" instead. There're no different, right? How about type "Ctrl-A" and "d"? You'll see your console again, with a word "[detached]". This means you have detached a session which is running the command "top".

Can't get the idea? Type "screen -ls", you'll see the sessions in the system.

$ screen -ls
There are screens on:
2963.pts-0.server (Detached)

The 'top' is still running, but in a detached session. You can attach the session anytime at anywhere. Just run "screen -r " (pid is 2963 in this case). Then the "top screen" is back again.

So next time when you want to run a command that may take a certain of time and you're not sure you can maintain the
console session, you can consider use "screen" to run in a session, detach it and reattach it later.

Ref: http://www.rackaid.com/resources/tips/linux-screen.cfm

No comments: