#!/usr/bin/ksh
#
# prc_chk.ksh - This script will check to see if the designated 
# process is active. If active, the script output is set to 1. 
# Also, if active, the elapsed time is checked to see if a
# set limit has been exceeded.
#
#
#	Program calls: None
#								
#								
#	Input arguments:
#
# 	$1 = Name of script
#		$2 = Elapsed run time limit
#
#
# Creation Date:
#
#								7 April 2008 TimQuinn
#
#	Updates:
#					
#	

#
## Set the environment variables
#

#	. ${THMSOC}/src/config/soc_it_to_me.config
    . /home/thmsoc/astraea2_test/soc_it_to_me.config

#
## Grab input variable
#
	script=$1
	limit=$2

#
## Check input variables
#
	if [[ -z $script ]]
	then
		echo "Please pass the name of a script......."
		exit 0
	fi

	if [[ -z $limit ]]
	then
		#set to default - 6 hours
		typeset -i limit=21600
	else
		typeset -i limit
	fi

#
## Set some local variables
#
	workstation=$(uname -a|awk '{print $2}')

#
## Check to see if script is running
#
	isrun=$(ps -C $script -f |grep $script)
	if [[ -n $isrun ]]
	then
		#script is running. check elapsed time
		typeset -i etime=$(${KSH_SCRIPTS}/prc_etime.ksh $script)
		if (( $etime > $limit ))
		then
			#run time limit exceeded - notify ops personnel
			printf "%s\n" "script: $0" \
			"Warning: Script $script has been running for $etime seconds on $workstation" |
			mailx -s "THEMIS Script Running Long" $SOC_EMAIL
		fi
		# signal script is running
		echo Script $script is running
		exit 1
	else
		# signal script is not running
		echo Script $script is not running
		exit 0
	fi
