Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: killing command if it takes too much time

by Argel (Prior)
on Feb 10, 2011 at 00:02 UTC ( #887317=note: print w/ replies, xml ) Need Help??


in reply to killing command if it takes too much time

If you are okay with (or want) to kill the command that's taking too long then eval+alarm may be what you are looking for. Maybe this code will give you some ideas:

sub cmd_wrapper { my $timeout = shift; my $cmd = shift; my( $out, $rc ); eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; $out = (`$cmd`)[0]; # Get first line of output $rc = $?; alarm 0; }; if( $@ ) { die "Eval failed for $cmd for unknown/unexpected reasons" if $ +@ ne "alarm\n"; die "Eval failed for $cmd because alarm timed out"; } die "Return code undefined for $cmd" unless defined $rc; return $rc, $out if wantarray; return $rc; } my $rc = cmd_wrapper(300, $pull_cmd); # Will timeout in 5 minutes (300 + seconds) # Add your logging here

Elda Taluta; Sarks Sark; Ark Arks


Comment on Re: killing command if it takes too much time
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://887317]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2015-07-30 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls