Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

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

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and dust plays in a shaft of sunlight...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (4)
    As of 2018-06-24 21:18 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.