Last active
December 14, 2015 03:38
-
-
Save zba/5021950 to your computer and use it in GitHub Desktop.
perl sleep test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ time ./test.sh | |
real 0m6.020s | |
user 0m0.004s | |
sys 0m0.008s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
sleep 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
a=`./timeout.pl 2 ./run_script.sh` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
changing
kill 9, $pid;
tokill 9, $PID;
not helps