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

Hello Monks, I have to invoke two commands parallely to collect their outputs in two different file. Currently I have below, which runs the commands one after the another:
&colstat; sub colstat { my $i=0; while($i<=10){ my $psout = '/my/long/path/memOut.txt'; my $vmstatOut = '/my/long/path/vmstatOut.txt'; system("date >> $vmstatOut"); system("vmstat 2 2 >> $vmstatOut"); system("date >> $psout"); system("ps auxw | grep -v grep >> $psout"); $i++; } }
How can this be done so that for each system date timestamp I get the output of both the above commands? Thanks, Xhings

Replies are listed 'Best First'.
Re: parallel command execution
by flexvault (Monsignor) on Oct 28, 2011 at 09:43 UTC

      How can this be done so that for each system date timestamp I get the output of both the above commands?

    I'm not sure I understand your question.

    If you want to get the commands to run in the background, just add a '\&' at the end of the system command:(AIX/Unix/Linux)

    system("date >> $vmstatOut \&"); system("vmstat 2 2 >> $vmstatOut \&");

    if you want get the results ever minute, put a 'sleep' command or run the script from cron.

    Good Luck!

    "Well done is better than well said." - Benjamin Franklin

Re: parallel command execution
by johnny_carlos (Scribe) on Oct 28, 2011 at 22:35 UTC
    Xhings You could try forking. fork

    This creates a second process of your script, both at the same place in your code. After that, test for one process, execute for file1.
    And then test for the other process, execute code for file2.

    Something like this:

    $pid = fork(); if( $pid == 0 ){ # This is one process(happens to be child) # Execute command for file 1 } else { # This is the second process(happens to be the parent) # Execute command for file 2 }
    I think both of those will run at the exact same time(as close as reasonably possible). Hope that helps. John