But alarm sends a signal to the process that called alarm, not the child process, right?

Unless the process that called alarm is the process that created child, and by killing the process you kill the child. Try alternating 5 and 2 in the script I have whipped up below (make sure you read the warning, if file xyz is written then child has terminated OK, if not it was timed out):

#!/usr/bin/env perl # for https://perlmonks.org/?node_id=1231710 # author: bliako # 27/03/2019 # WARNING: it writes and deletes file 'xyz' in local dir use strict; use warnings; sub spawner { my ($cmd, $timeout) = @_; print "spawner starting.\n"; my $pid = fork(); if( ! $pid ){ print "spawner in child.\n"; eval { local $SIG{ALRM} = sub { die "alarm blah blah" }; alarm $timeout; print "starting system command.\n"; `$cmd`; print "system command ended.\n"; alarm 0; }; if( $@ =~ /^alarm blah blah/ ){ die "spawner: child timed out" + } elsif( $@ ){ die "spawner: something wrong with eval block whi +ch spawns '$cmd'" } exit 0; } print "spawner ending.\n"; } spawner('rm -f xyz; sleep 5; touch xyz', 2); wait(); if( -e 'xyz' ){ print "command seems to have terminated on its own wil +l.\n"; } else { print "command has timed out.\n"; }

bw, bliako


In reply to Re^3: non-blocking backticks by bliako
in thread non-blocking backticks by chris212

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.