public
Last active

perl sleep test

  • Download Gist
output
1 2 3 4 5
$ time ./test.sh
 
real 0m6.020s
user 0m0.004s
sys 0m0.008s
run_script.sh
Shell
1 2
#!/bin/sh
sleep 6
test.sh
Shell
1 2
#!/bin/sh
a=`./timeout.pl 2 ./run_script.sh`
timeout.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/usr/bin/perl
use strict;
use warnings;
my $PID=$$;
my $timeout=shift;
my @args=@ARGV;
 
my $pid = fork();
defined $pid or die "fork: $!";
$pid == 0 && exec(@args);
 
my $timed_out = 0;
$SIG{ALRM} = sub { $timed_out = 1; die; };
alarm $timeout;
eval { waitpid $pid, 0 };
alarm 0;
if ($timed_out) {
print "Timed out!\n";
kill 9, $pid;
kill 9,$PID;
}
elsif ($@) {
warn "error: $@\n";
}

changing kill 9, $pid; to kill 9, $PID; not helps

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.