19 November 2007

How to manage interactive programs that run for long periods of time

Problem Statement:

A user wants to run a program 'a' that takes seven hours to complete.He wants to start the program while leaving his office in the evening.But he feels that power will be wasted if he invokes this program in the evening,and keeps the workstation that has started the program 'a' through the SSH client switched 'on',just to keep 'a' running.

User can try the following solutions:

SOLUTION1:


$cd /path/to/a
$
sh a.sh &
$
logout

Disadvantage of this option is that it will not capture the exit code of 'a'

SOLUTION2:

$screen
$
cd /path/to/a
$
sh a.sh

Shutdown the machine that has started the program 'a' using ssh.

Next day,invoke the ssh session to the system and retrieve the same terminal that has started this long-running program 'a',using the following technique:

$screen -list

This will list the set of screen managers - in the above case you should see only one screen manager,something like the following:

This is a screen on:

There is a screen on:
23399.pts-1.maya-replace (Detached)

1 Socket in /tmp/screens/S-root.


Now , the following command should get the screen back.

$screen -d -r 23399.pts-1.maya-replace


No comments: