Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

How to run a command, gather its output and kill it after a specified time?

by blindluke (Hermit)
on Feb 15, 2012 at 13:49 UTC ( #953951=perlquestion: print w/replies, xml ) Need Help??
blindluke has asked for the wisdom of the Perl Monks concerning the following question:

Enlightened Monks!

I'm trying to run a command that runs some kind of interactive shell, executes a script, and then stays in the shell. It's stubborn, and does not exit the shell, even if I explicitly provide an exit command in the script file. Also, it hangs frequently, so I cannot be sure that for a given script, I will receive any output (so I cannot use a solution that would open the command with a pipe, then read first X lines, then kill it).

Right now, I'm trying to adopt the idea found here: Re: killing command if it takes too much time. I want all the lines, so I modified it a bit. This is the code after my modifications, and the way I use this sub later:

sub cmd_wrapper { my $timeout = shift; my $cmd = shift; my @out; # all lines of output my $return_code; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; @out = (`$cmd`); $return_code = $?; 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 $return_code; return $return_code, @out if wantarray; return $return_code; } @cmdoutput = cmd_wrapper(15, "my_stubborn_shell -c $filename"); # Will + timeout in 15 seconds $cmd_ret_code = shift @cmdoutput;

This, however, does not work. It just times out with "Eval failed because alarm timed out". How can I gather the output, and then kill the program, after a given time (let's say, 20 seconds)?

I could always do it with Expect (especially since I can exit the shell without problems when in interactive mode), but maybe there is an easier solution. If Expect is the right way to do this, please, let me know.

Luke Jefferson

  • Comment on How to run a command, gather its output and kill it after a specified time?
  • Download Code

Replies are listed 'Best First'.
Re: How to run a command, gather its output and kill it after a specified time?
by moritz (Cardinal) on Feb 15, 2012 at 14:00 UTC

    Maybe I'm missing the point here, but if you don't want the script to die in case the alarm triggers, maybe just remove the respective call to die?

    I also recommend looking at IPC::Run, which also has timeouts.

      I want the script (my_stubborn_shell) to be killed after the timeout (as this script does not exit gracefully), but then, when it's killed, I want to have the output that my_stubborn_shell sent to STDOUT, or an indication that it did not print anything.

      Luke Jefferson

Re: How to run a command, gather its output and kill it after a specified time?
by JavaFan (Canon) on Feb 15, 2012 at 15:44 UTC
    You already kill the external program. You then decide to kill the main program when that happens. If you don't want to die, then, well, don't.

      Thank you, JavaFan! Of course, moritz already gave the right suggestion, but I was as stubborn as the shell in question in not understanding what he said. :)

      My sincere thanks for the answer, and for having the necessary patience to repeat it for me.

      Luke Jefferson

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://953951]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2018-05-21 15:00 GMT
Find Nodes?
    Voting Booth?