X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=runcheckdir;h=d6589f2f5c8716a06c0c29636933bbd6911402ca;hb=98307380429da3ad6e18c99200da6449719dc106;hp=4964a4cf2a72cdfde634777b07c45a801617f099;hpb=04dd40d98617a67de54f661482f2955acc4a1d52;p=chksht.git diff --git a/runcheckdir b/runcheckdir index 4964a4c..d6589f2 100755 --- a/runcheckdir +++ b/runcheckdir @@ -30,7 +30,7 @@ sub do_command { } -my ($checkcommand, %on); +my ($checkcommand, $every, %on); $on{success} = []; $on{failure} = []; $on{change} = []; @@ -52,6 +52,8 @@ while () { } else { print "Unknown event in 'on', $dir/check line $.\n"; } + } elsif ($command eq 'every') { + $every = $words[0]; } else { print "Unknown command '$command', $dir/check line $.\n"; } @@ -68,27 +70,43 @@ unless (@{$on{failure}} || @{$on{success}} || @{$on{change}}) { open STATUS, "$dir/checkstatus"; chomp(my $oldstatus = ); +my $oldtime = (stat(STATUS))[9]; close STATUS; -open CHECK, '-|', $checkcommand; +my $td = time() - $oldtime; +if ($td < $every) { + # Not time to recheck yet + #print "Not rechecking; ", $every - $td, " seconds to go.\n"; + exit $oldstatus; +} + +my ($pid, $hung, $exitstatus); +$SIG{ALRM} = sub { + kill 9, $pid; + $hung = 1; +}; +alarm 10; + +$pid = open CHECK, '-|', $checkcommand; $status = ; $details = join('', ); close CHECK; -my $exitstatus = $? >> 8; - -if ($exitstatus == 0) { - foreach my $command (@{$on{success}}) { - do_command $command; - } +if ($hung) { + $status = 'TIMEOUT'; + $details = 'Check did not complete within ten seconds'; + $exitstatus = -1; } else { - foreach my $command (@{$on{failure}}) { - do_command $command; - } + $exitstatus = $? >> 8; +} + +if ($exitstatus == 0 && $oldstatus != 0) { + do_command($_) foreach @{$on{success}}; +} +if ($exitstatus != 0 && $oldstatus == 0) { + do_command($_) foreach @{$on{failure}}; } if ($exitstatus != $oldstatus) { - foreach my $command(@{$on{change}}) { - do_command $command; - } + do_command($_) foreach @{$on{change}}; } open STATUS, ">$dir/checkstatus"; @@ -96,3 +114,5 @@ print STATUS "$exitstatus\n"; print STATUS "$status\n"; print STATUS "$details\n"; close STATUS; + +exit $exitstatus;