Hello gurus-

I have been wracking my brain with this one, and can't get it to perform quite how I want it to. My goal is to have a script that runs a system call and if it receives a SIGINT (CTRL+C), kill the system call along with the pl script. Here is an oversimplified example:
$SIG{INT}=\&myhand; print "Parent pid = $$ \n"; $pid = open(CMD, "ping google.com -n 15 |"); print "Ping running... pid = $pid \n\n"; # uncommenting this line breaks ability to SIGINT #print <CMD>; # keep the script running; only the SIGINT will exit while (1){sleep 1}; sub myhand() { print "Parent $$ caught SIGINT.\n"; print "Parent $$ will KILL $pid.\n"; kill 9, $pid; exit(0); }
The script works fine to kill the ping until you uncomment the print. Then it will not accept the SIGINT until the ping has completed.

It seems like this would be a pretty common request. I know that system() calls will ignore SIGINT. Am I making this too complicated, or not complicated enough? I even tried with fork(), but I don’t think I am doing it properly:
#parent if ($forkpid = fork) { local $SIG{INT} = sub { print "i'm quitting"; kill 9, $pid; exit; }; waitpid($forkpid, 0); #child } else { die "cannot fork: $!" unless defined $forkpid; $SIG{INT} = sub { print "i'm quitting child"; close(CMD); kill 9, $p +id; exit; }; $pid = open(CMD, "ping google.com -n 15 |"); # exec ("ping google.com -n 15") or die "Can't exec: $!\n"; } print "Done with fork. Here are my results:"; print <CMD>;
I seem to have the same problem if I use open(). If I use exec(), it looks like the ping command itself accepts the CTRL+C?

I would be extremely appreciative of any assistance you could offer!

In reply to SIGINT and system calls by Anonymous Monk

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.