C
Chris
I am trying to read some data from another process, let’s call the
program foo (binary and I don’t have the source). It works like ping in
that it spits output to the screen every few seconds and has to be
killed with control-c. I can write a script to read data from ping and
kill the ping after say 10 seconds. But I don’t get any data from foo
even though it behaves just like ping at the command prompt. (I even
tried sending the data to a file but data doesn’t show up until 4096
bytes are written and that is too long to wait.) I’ve tried all kinds of
things like setting the file desc to non-blocking, using sysread, and
even fork (open FD, “-|”) with foo as the child, but still no luck. Any
ideas are appreciated. What is the shell doing to get the buffered
output from foo on a line-by-line basis? Here is what I have that works
for ping (on linux).
Thanks, Chris
eval
{
local $SIG{'ALRM'} = sub { die "alarm\n"; };
alarm 10;
unless(open(FD, "/bin/ping myhost |"))
{
die "Unable to open pipe\n";
}
while($line = <FD>)
{
print "$line";
}
unless(close(FD))
{
die "Unable to close pipe\n";
}
alarm 0;
};
local $SIG{'INT'} = 'IGNORE';
kill INT => -$$;
program foo (binary and I don’t have the source). It works like ping in
that it spits output to the screen every few seconds and has to be
killed with control-c. I can write a script to read data from ping and
kill the ping after say 10 seconds. But I don’t get any data from foo
even though it behaves just like ping at the command prompt. (I even
tried sending the data to a file but data doesn’t show up until 4096
bytes are written and that is too long to wait.) I’ve tried all kinds of
things like setting the file desc to non-blocking, using sysread, and
even fork (open FD, “-|”) with foo as the child, but still no luck. Any
ideas are appreciated. What is the shell doing to get the buffered
output from foo on a line-by-line basis? Here is what I have that works
for ping (on linux).
Thanks, Chris
eval
{
local $SIG{'ALRM'} = sub { die "alarm\n"; };
alarm 10;
unless(open(FD, "/bin/ping myhost |"))
{
die "Unable to open pipe\n";
}
while($line = <FD>)
{
print "$line";
}
unless(close(FD))
{
die "Unable to close pipe\n";
}
alarm 0;
};
local $SIG{'INT'} = 'IGNORE';
kill INT => -$$;