
$| = 1;

$ENV{LD_LIBRARY_PATH} = "/homes/tmunson/jtc/lib:/homes/tmunson/ooc/lib:/soft/gnu/packages/gcc-2.95.2b/lib/gcc-lib/XSi686-pc-linux-gnu/2.95.2:/soft/gnu/packages/gcc-2.95.2b/lib";


#
# Restart the server if the last instance of "kestrel-checker"
# killed everything, in which case the file $RESTART
# will exist.
#

$RESTART = "$ENV{SERVER_TMP}/kestrel-checker-restart";


if (-f "$RESTART") {

  #
  # Notify someone of the restart
  #

  chop($TIME = `date`);
  open(handle, "|Mail -s '$ENV{SERVER_NAME} kestrel server restart' $ENV{SERVER_ADMIN}");
  print handle "checker: $ENV{SERVER_NAME} kestrel server restarted on $TIME"; 
  close(handle);
  print "kestrel-checker: restarting $ENV{SERVER_NAME} kestrel server on $TIME\n"; 

  #
  # Restart server
  #

  system("$ENV{SERVER_USER_BIN}/kestrel-start 2>>$ENV{SERVER_VAR}/logs/kestrel_errors");

  unlink "$RESTART";
  exit(0);
}

if(open(READPID, "$ENV{SERVER_VAR}/proc/kestrel-server.pid"))
{
    chomp($kpid = <READPID>);
    close READPID;

#
# Send 0 signal to determine whether process is still alive.
#

    unless(kill 0 => $kpid) {
	print "kestrel-checker: No response from $kpid\n";
	open(HANDLE, ">$RESTART");
	close HANDLE;
    }

} else {
    print "kestrel-checker: No file in place $ENV{SERVER_VAR}/proc/kestrel-server.pid.\n";
    open(HANDLE, ">$RESTART");
    close HANDLE;
}
