#! /bin/sh 
#$Id: pvmjob,v 5.3 1996/12/04 19:42:16 jwang Exp $
#  This sample script is a wrapper for running pvm jobs under lsbatch.
#  Submit job by saying "bsub [otheroptions] -n k pvmjob command line",
#  where k is the number of hosts to use; pvmjob is the name of this script.
#
#  Note the variable LSB_HOSTS is assigned by lsbatch system when this script
#  is started by lsbatch.

# LSB_TRAPSIGS is the platform specific trap for signals. This script does not
# want to die unless its members all die.
$LSB_TRAPSIGS 

HOSTFILE=$HOME/.lsbatch/host$$.`hostname`

CMD=$1

COMMANDLINE="$@"

rm -f $HOSTFILE
touch $HOSTFILE

#Generate hostfile
nhosts=0
for word in $LSB_HOSTS 
do
echo $word >> $HOSTFILE
nhosts=`expr $nhosts + 1`
done


#unset PVM_ARCH environment variable
PVM_ARCH=""; export PVM_ARCH

# start pvm daemons
pvmd $HOSTFILE &

# wait until daemons started.
sleep `expr $nhosts "*" 6`

# run pvm job and save exit status.
eval $COMMANDLINE
exstat=$?

# Kill remote tasks that do not die.
pvm << RESET
reset
RESET

#Kill pvm daemons and clean up hostfile
pvm <<HALT
halt
HALT

# Allow pvmd to cleanup
sleep 5

# Kill runaway processes
lsgrun -p -f $HOSTFILE zapit -9 pvm pvmjob >/dev/null 2>&1
lsgrun -p -f $HOSTFILE zapit -9 $CMD pvmjob >/dev/null 2>&1

rm -f $HOSTFILE

exit $exstat

