
#
# Start Kestrel Server
#

$val = system("$ENV{SERVER_USER_BIN}/kestrel-kill 2>> $ENV{SERVER_VAR}/logs/kestrel_errors");
 
if($val == 0) {
    $command = "($ENV{KESTREL_HOME}/kestrel_server  -ORBthreaded -OAhost neos.mcs.anl.gov -OAport 9000 -OAthread_per_client >> $ENV{SERVER_VAR}/logs/kestrel) 2>> $ENV{SERVER_VAR}/logs/kestrel_errors";

    setpgrp(0, $$);
    system("echo $$ > $ENV{SERVER_VAR}/proc/kestrel-server.pid");
    $childid = fork();
    if (!$childid) {
	exec($command);
    } else {
	{
	    local $SIG{'ALRM'} = \&cleanup;
	    waitpid($childid, 0);
	}

    }

} else {
    print STDERR "Old server could not be killed for restart\n";
}

sub cleanup {
    $id2kill = getpgrp();
    print "Kill child group $id2kill\n";
    kill -9, $id2kill;
    exit 1; #in case I'm not already dead, which I should be
}

