Skip to content

Instantly share code, notes, and snippets.

@zba
Last active December 14, 2015 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zba/5021950 to your computer and use it in GitHub Desktop.
Save zba/5021950 to your computer and use it in GitHub Desktop.
perl sleep test
$ time ./test.sh
real 0m6.020s
user 0m0.004s
sys 0m0.008s
#!/bin/sh
sleep 6
#!/bin/sh
a=`./timeout.pl 2 ./run_script.sh`
#!/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";
}
@zba
Copy link
Author

zba commented Feb 24, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment