[r16068]: rvmroot / branches / RVM-127-JMX / bin / timedrun Maximize Restore History

Download this file

timedrun    67 lines (59 with data), 1.4 kB

#!/usr/bin/perl
#
#  This file is part of the Jikes RVM project (http://jikesrvm.org).
#
#  This file is licensed to You under the Common Public License (CPL);
#  You may not use this file except in compliance with the License. You
#  may obtain a copy of the License at
#
#      http://www.opensource.org/licenses/cpl1.0.php
#
#  See the COPYRIGHT.txt file distributed with this work for information
#  regarding copyright ownership.
#
require "getopts.pl";
&Getopts('t:e:');

$alarm_exit_code = 1;

if ($opt_t ne "") {
    $SIG{ALRM} = 'alarmHandler';
    $deadman_seconds = $opt_t;
}

if ($opt_e ne "") {
    $alarm_exit_code = $opt_e;
}

FORK: {
    if ($pid = fork) {
	# parent
	if (defined ($deadman_seconds)){
	    alarm($deadman_seconds);
	}
	wait;
        exit($? >> 8);
    } elsif (defined $pid) { # $pid is zero here if defind
	# child
	exec @ARGV;
    } elsif ($! =~ /No more process/) {
	# EAGAIN, supposedly recoverable fork error
	sleep 5;
	redo FORK;
    } else {
	# weird fork error
	die "Can't fork: $!\n";
    }
}

sub alarmHandler {
    print "CAUGHT ALARM\n";
    system("kill  $pid 2>/dev/null");
    sleep 2;
    euthanase($pid);
    exit($alarm_exit_code);
}

sub euthanase {
    my ($pid) = @_;
    open (JOBS, "ps -ef | cut -c10-21 | grep $pid|");
    while (<JOBS>) {
	($target) = /(\d+)\s+\d+/;
	system("kill -9 $target 2>/dev/null");
    }
    close JOBS;
}