in reply to Re: Reading progress of "copy" executed asynchronously
in thread Reading progress of "copy" executed asynchronously
#!/usr/bin/perl -w use strict; use IPC::Open2; use IO::Select; $| = 1; # autoflush STDOUT # Declare filehandles and command to use: my ($r, $w); my $cmd = 'ping 127.0.0.1'; # Open the process and set the selector: my $pid = open2($r, $w, $cmd); my $selector = IO::Select->new($r); while (1) # infinite loop (use "last" to break out) { if ($selector->can_read(0)) { my $chars; my $bytes_read = sysread($r, $chars, 4046); print "$bytes_read $chars\n"; } # Do anything you want in between reads here... } __END__ The advantage to this script is that, if your commands (like "whois", "dig", and "ping") happen to pause, the loop won't automatically break out. The disadvantage to this script is that it might be difficult figuring out when a command has finished, or just has delayed output (in which case you might have to put in a few sleep() calls). Either way, I think that this script here does a better job of helping you visualize what is going on -- you just need to be mindful of the fact that some programs don't flush their output right away, and that it's not a simple matter to tell if the program has stopped running altogether.
|
|---|