#!/usr/bin/perl # # run a command with a deadman timer (kill it if it runs longer than # the specified amount) # # jwa@jammed.com 6 dec 2000 # (because ftp has no timeout mechanism!) # ($secs, @args) = @ARGV; if ($secs =~ /m$/) { # handle '5m' or whatever $secs =~ s/m$//g; $secs = $secs * 60; } if ($secs !~ /^\d+$/) { # but that's all I do for now print "Use seconds or 'm'\n"; exit (1); } if (!@args) { print "usage: $0 seconds arg1 [arg2 argn..]\n"; exit 0; } if ($pid = fork()) { # Parent $SIG{ALRM} = sub { print "$0 killing process '" . join(" ", @args) . "' after $secs seconds\n"; kill 9, $pid; exit(99); }; alarm($secs); } else { # child exec(@args) || die "$0 - exec failed: $!"; } wait(); $code = $?; alarm(0); exit $code / 256;