Can't get output from pipe with autoflush on

I

Ishmael

Howdy,

I'm trying to get a list of clients/users on my network using 'rusers'.
When I run 'rusers' from the command line, the behavior is as follows:
First, it instantaneously prints list of clients/users (perfect) THEN
it hangs for several seconds (I think this is due to a problem with the
client database configuration, over which I have no control). In any
case, I would like to time out the function after a second or so, keep
the first chunk of output, and kill the 'rusers' function.
Unfortunately, it seems as if the output does not become accessible via
the filehandle (see below) unless 'rusers' exits normally (which takes
too long). I've tried turning 'autoflush' on, but this doesn't seem to
work.

Any help would be greatly appreciated! Here's the code:

undef $/; # Try eliminating OUTPUT_RECORD_SEPARATOR

print "Getting user list via rusers\n";
my ($pid, $rusers_out, @tmp);
eval {
local $SIG{ALRM} = sub { die "alarm clock restart\n" };
alarm $rusers_timeout;
eval {

$pid = open F, "rusers -a |" or die "rusers gave error: $!";
F->autoflush(1);
$rusers_out = <F>;

};
alarm 0;
if ($@) {
if ($@ =~ /alarm clock restart/) {
die "'rusers' timed out.\n"; # Create an exception to get out of
outer loop.
}
else {
die($@);
}
}
};
alarm 0;

if ( $@ ne '' ) {
print "$@";

# kill PID with a TERM (terminate) signal. Note: Control-C (INT
signal) also works.
my $num_successfully_killed = kill('TERM', $pid);

if ($num_successfully_killed != 1) {
print "Error: Could not kill rusers after timeout\n";
}
}

print "RUSERS OUTPUT: $rusers_out\n"; # Nothing gets here
exit;
 
X

xhoster

Ishmael said:
Howdy,

I'm trying to get a list of clients/users on my network using 'rusers'.
When I run 'rusers' from the command line, the behavior is as follows:
First, it instantaneously prints list of clients/users (perfect) THEN
it hangs for several seconds (I think this is due to a problem with the
client database configuration, over which I have no control). In any
case, I would like to time out the function after a second or so, keep
the first chunk of output, and kill the 'rusers' function.
Unfortunately, it seems as if the output does not become accessible via
the filehandle (see below) unless 'rusers' exits normally (which takes
too long). I've tried turning 'autoflush' on, but this doesn't seem to
work.

You need to get rusers to turn on *its* autoflush. If you trick it into
thinking it is talking to a terminal rather than program, it might do so.
I think the Expect module can arrange this trickery for you.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top