Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

termination of system command

by Anonymous Monk
on Feb 11, 2008 at 14:59 UTC ( [id://667390]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,
how to terminate command given specific time
if you see my code i can't come to second line untill kill the vmstat command
@val = `vmstat 10`; foreach $_ (@val) {print "$_\n";}
can any body help me out on this

Replies are listed 'Best First'.
Re: termination of system command
by Fletch (Bishop) on Feb 11, 2008 at 15:06 UTC

    In general one can use alarm (see also perlipc) to arrange for a signal to be delivered after a specified delay.

    However in this specific case most implementations of vmstat should also take a count argument as well as a delay; start it with (say) vmstat 10 3 and you should only wait 20 seconds for 3 measurements (the first is immediate so you only get 2 delay periods).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: termination of system command
by svenXY (Deacon) on Feb 11, 2008 at 15:21 UTC
    Hi,
    you can also open it with '-|' (pipe from vmstat) and then use a while-loop as shown below:
    #!/usr/bin/perl use strict; use warnings; open(STAT, '-|', 'vmstat 2') or die "Could not open pipe: $!"; my $count; # not necessary, just for demonstration while (<STAT>){ last if $count++ > 5; # we don't want more than 5 lines of out +put print $_, "\n"; } print "...done\n";

    Regards,
    svenXY
Re: termination of system command
by kyle (Abbot) on Feb 11, 2008 at 15:56 UTC
Re: termination of system command
by tcf03 (Deacon) on Feb 11, 2008 at 16:27 UTC
    This does not answer your question directly, but why reinvent a wheel here? vmstat will stop itself using it like vmstat delay count. Using the following vmstat will stop collecting after 10 iterations at 10 second intervals.
    @val = `vmstat 10 10`;


    update - system or IPC::Run3 may serve you better.

    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found