Hi there!

So I need to read the output of a command that outputs a lot of data... but I don't need all the data. I want to be able to stop after some point.

For just a single pipe to my script this works OK:

$pid = open (INPUT, "mycommand $somefile |") or die ...; while (<INPUT>) { if ( ... trigger to stop ... ) { kill 9, $pid; last; } } close INPUT;

At the point the kill is issued, mycommand is terminated, life is good and we are ready to move on and process the next file.

... by the way, I'm on Windows XP, ActiveState Perl 5.8.8.

I'm not even sure what signal I should be sending with the kill (I tried 1, 2 and 9... 9 worked so that's what I am using).

What I want to do is:

$pid = open (INPUT, "gzip -dc $somegzfile | mycommand |") or die ...; while (<INPUT>) { if ( ... trigger to stop ... ) { kill 9, $pid; last; } } close INPUT;

Notice the pipe to a pipe... "gzip ... | mycommand |"

This works in the sense that it gunzips the file on the fly, and passes the data to mycommand, and then I get what I want in the script.

BUT it does not kill the gzip or mycommand processes when it gets to the "kill 9, $pid". Then it just sits at the close INPUT until the gzip and mycommand are done (which takes a few minutes).

Any ideas on how I should do the piping so I can kill the processes properly? Also, any suggestions on what signals to use in windows?

Thanks!
David A.


In reply to open (to read) and kill a pipe to a pipe 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.