Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-28 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found