Can't get output from pipe with autoflush on

Discussion in 'Perl Misc' started by Ishmael, Jan 18, 2007.

  1. Ishmael

    Ishmael Guest

    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;
     
    Ishmael, Jan 18, 2007
    #1
    1. Advertising

  2. Ishmael

    Guest

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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jan 19, 2007
    #2
    1. Advertising

  3. Ishmael

    Ishmael Guest

    Thanks for the help! The Expect module is fantastic!
     
    Ishmael, Jan 21, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Herman Chan

    CGI autoflush in Window

    Herman Chan, Oct 9, 2003, in forum: Perl
    Replies:
    0
    Views:
    583
    Herman Chan
    Oct 9, 2003
  2. dede
    Replies:
    0
    Views:
    591
  3. Gianluca Trombetta

    Autoflush in python cgi

    Gianluca Trombetta, Jun 1, 2004, in forum: Python
    Replies:
    1
    Views:
    1,005
    Andrew Dalke
    Jun 2, 2004
  4. John Chambers
    Replies:
    2
    Views:
    187
    John Chambers
    Jan 23, 2004
  5. Axel Boldt
    Replies:
    1
    Views:
    137
    Dan Wilga
    Apr 7, 2004
Loading...

Share This Page