#!/usr/bin/perl # safe - run a command but kill it after the given amount of time # Steve Kinzler, kinzler@cs.indiana.edu, Jun 08 # ported/enhanced from safe(1) in C by Mike Blackwell, ihnp4!aicchi!mdb, Aug 86 # http://www.cs.indiana.edu/~kinzler/home.html#unix # Tests: ./safe -d 3 /bin/csh -fc : && echo ok # ./safe -d 3 sleep 1 && echo ok # ./safe -d 3 sleep 5 || echo ok # NOTE: This seems to mostly work, but I'm not entirely confident with it # since I've seen it fail strangely on occasion on RH9 and RHEL3. $dflt_d = 60; $usage = < 0) { print STDERR "\a\a", $opt_b ? '' : "$0: killing process $pid in $opt_w seconds\n"; sleep $opt_w; } &maim(); } sub maim { $SIG{'CHLD'} = 'IGNORE'; kill(15, $pid); sleep(2) if kill(0, $pid); kill( 9, $pid) if kill(0, $pid); sleep(2) if kill(0, $pid); if (kill(0, $pid)) { warn "$0: process $pid will not die\n"; } else { warn "$0: process $pid killed\n" unless $opt_q; } exit 1; } sub done { warn "$0: process self-terminated\n" if $opt_v; exit 0; }