#!/usr/bin/sh
#
# file: $LINKON/src/v2.0/util/run_monitor/run_monitor.sh
#
# This script watches until the process given by $1 until it no
# longer appears in the ps output, and then sends mail to $2
#
# a typical example is:
#
#    run_monitor.sh 13742 admin@isip.msstate.edu Data collection system 1
#
# arguments:
#    $1 is the process ID
#    $2 is the mail alias to alert when the program dies
#    $3-$9 are additional fields that will be included in the mail
#          message.
#
#
PID="$1";
EXEC_NAME="$0";

# check if the user supplied process id exists
#
PID_EXISTS=`ps -ef|cut -b9-15|grep $PID`;

# PID_EXISTS will be null if the process does not show up on ps -ef
#
if (test -n "$PID_EXISTS") then

    # process is still running, wait and recall run_monitor.sh
    #
    # note: exec causes this instance of run_monitor to exit
    #
    sleep 180;
    exec $EXEC_NAME $PID "$2" $3 $4 $5 $6 $7 $8 $9
fi

# process id did not show up in ps, mail administrator
#
# important definitions go here
#
# note: these definitions could be made anywhere in the script, but
#       it is most efficient to make them here, since they will not
#       be run everytime the script is run
#
ALIAS=$2;
PROG_NAME="$3 $4 $5 $6 $7 $8 $9";
MAIL=/bin/mail;

# note: date executed here finds the date of program termination
#
DATE=`date`;
SUBJECT="Subject: run monitor for pid $PID";

# send the mail
#
echo "$SUBJECT\nProgram $PROG_NAME (pid = $PID) died on:\n $DATE"| $MAIL $ALIAS
	
