#!/bin/csh
#
#	Stage Manager Watchdog
#
#
#	CVS ID: Stage_Manager_Watchdog,v 1.4 2008/10/20 08:58:14 castalia Exp

set STAGE_MANAGER	= /opt/local/sh/Stage_Manager
set CONFIG_FILE		= /etc/Stage_Manager.conf
set LOG_DIR			= /var/log/Stage_Manager
set LOG_FILE		= $LOG_DIR/Stage_Manager.log
set PID_FILE		= $LOG_DIR/Stage_Manager.pid
set POLL_INTERVAL	= 10


#	Initialize the PID; from an existing PID file if available.
if (-f $PID_FILE) then
	set PID = `cat $PID_FILE`
else
	set PID = $STAGE_MANAGER
endif

while (1)
	#	Watchdog: Will only exit if the Stage_Manager can not be started.

	#	Check if the process is running.
	set PIDs = (`/bin/ps auxwww \
		| /bin/grep -v grep \
		| /bin/awk '{print $2}' \
		| /bin/grep "$PID"`)
	while ($#PIDs)
		if ($PIDs[1] == $PID) then
			break
		endif
		shift PIDs
	end

	if (! $#PIDs) then
		#	The process was not found. Attempt to start the $STAGE_MANAGER.

		#	Remove the PID file.
		/bin/rm -f $PID_FILE

		#	Run the Stage_Manager and capture its PID.
		echo >>! $LOG_FILE
		/bin/date "+%e %B %G %T %z" >> $LOG_FILE
		$STAGE_MANAGER -Configuration $CONFIG_FILE >>& $LOG_FILE &
		set PID = $!

		#	Confirm that the Stage_Manager started successfully.
		sleep 3
		set PIDs = (`/bin/ps auxwww \
			| /bin/grep -v grep \
			| /bin/awk '{print $2}' \
			| /bin/grep "$PID"`)
		while ($#PIDs)
			if ($PIDs[1] == $PID) then
				break
			endif
			shift PIDs
		end

		#	Update the PID file.
		if ($#PIDs) then
			echo $PID >! $PID_FILE
		else
			echo "The Stage_Manager did not start successfully" >! $PID_FILE
			exit 1
		endif

	endif

	#	Wait for the poll interval.
	sleep $POLL_INTERVAL

end
