I came across this looking to do the same thing. Unfortunately, none of the posted solutions worked correctly for me. After testing, I found that it is essential to use "setpgrp(0,0)" in the child, otherwise the kill seems to be ignored. Also, it's unclear what would happen if the child has exec'd something which is hung in an unkillable fashion, so I'll attach a couple of working solutions here. First solution does an alarm and a kill, and doesn't use any fancy libraries.
#!/usr/bin/perl use strict; sub sys_alarm { my ( $cmd_result, $pid ); my $cmd = shift( @_ ); my $timeout = shift( @_ ); eval { local $SIG{ ALRM } = sub { kill -9, $pid; warn "timeout of command \"$cmd\"; killing $pid\n"; }; if ($pid = open(IN, "-|")) { alarm $timeout; while ( <IN>) { $cmd_result .= $_; } close(IN); waitpid $pid, 0; alarm 0; } else { die "cannot fork: $!" unless defined $pid; setpgrp( 0, 0 ); exec( $cmd ); exit; } }; return $cmd_result; } my $x = sys_alarm("sleep 10; echo foo", 5); print "Output: " . $x . "\n\n";
The second solution simply alarms and moves on, and leaves the child to do whatever it will. This may be preferable if there is a potential of unkillable processes. I have the impression that the previous code will not move on if the child does not terminate upon being killed.
#!/usr/bin/perl use strict; sub sys_alarm { my $cmd_result; my $cmd = shift( @_ ); my $timeout = shift( @_ ); eval { local $SIG{ALRM} = sub {die "alarm"}; alarm $timeout; $cmd_result = `$cmd`; alarm 0; }; if ($@) { print STDERR "Command \"$cmd\" timed out.\n"; return undef; } else { return $cmd_result; } } my $x = sys_alarm("sleep 10; echo foo", 5); print "Output: " . $x . "\n\n";

In reply to Re^2: fork - alarm - output by ikarius
in thread fork - alarm - output by chart3399

Title:
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, details, 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, summary, 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.