Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Capturing output from a "moving" system call

by DreamT (Pilgrim)
on Sep 24, 2009 at 16:37 UTC ( [id://797251]=perlquestion: print w/replies, xml ) Need Help??

DreamT has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I often make a system call by using backticks, like this:
my $output = `cat /proc/loadavg`; print $output;

, which works fine. But, If I want to capture the output from a "continous" application, like ping or top, how can I do that? Since these applications runs "forever" until you tell them to quit?

Replies are listed 'Best First'.
Re: Capturing output from a "moving" system call
by Anonymous Monk on Sep 24, 2009 at 16:45 UTC
    use IPC::Run, or IPC::Open, and read one line at a time
Re: Capturing output from a "moving" system call
by hippsta (Acolyte) on Sep 24, 2009 at 17:29 UTC
    Try it like this:

    #!/usr/bin/perl

    use warnings;
    use strict;

    my $cmd = 'top';

    open(TOP, "-|", "$cmd");

    while (<TOP>)
    {
    print "$_";
    }
    close (TOP);

    :)

    Respectfully,

    Brother Hippsta
Re: Capturing output from a "moving" system call
by saberworks (Curate) on Sep 24, 2009 at 20:01 UTC
    If you just need the info from top, for example, you can just run top in logging mode with a single sample: top -l 1

    Most command line tools like top and ping can just do whatever it is then exit, you don't necessarily have to use them in interactive mode.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://797251]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-16 17:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found