Win32::Process::Create() - how to capture process output?

Posted on 2004-11-23
Last Modified: 2008-10-06
Here is the progression of events... (1) I have an open logfile,  (2) I start a process using Win32::Process::Create,  (3) I "Wait" midway through process execution time, then  (4) print other stuff to the logfile.

I need to collect and print the output started at step 2 to the logfile.  I don't mind using a temporary file for storage...but prefer to capture data in memory.  How do I do either of these?  I'm thinking simple redirection to a file in the Process::Create( ) call won't work...but have not tried it.

sub call_it
  # ...
  $proc = execute_child($profile);
  proc->Wait (60 * 1000) # wait 1 minute
  print_file_sizes($profile); // prints some other data to the open logfile
  # ...
sub execute_child
  # ...
  Win32::Process::Create ($ProcessObj2, $proc_fname, "$PROC_NAME -int $interval -samples 3",
    0, NORMAL_PRIORITY_CLASS, "." );  
    # $PROC_NAME runs for 2 minutes - output normally goes to stdout ... its a command line process.
    # I would like to capture this output somehow...?
  # ...
  return $ProcessObj2;
Question by:mark_at_acc
    LVL 6

    Expert Comment

    If you redirect the process output into the file, does it work? if it is, that might  be the simplest method here.

    "$PROC_NAME -int $interval -samples 3 > c:\\proclog.txt"
    LVL 6

    Accepted Solution

    You can as well redirect the STDOUT output for the process into a file.

    It is heard to explain it better than it is done here:

    LVL 18

    Expert Comment

    why not use backticks or qx?

        $output = qx/$PROC_NAME -int $interval -samples 3/;


        $output = `$PROC_NAME -int $interval -samples 3`;

    Author Comment

    Hi kanduru, I cannot use backticks since I need to be able to continue processing lines in the perl script...using backticks halts the script until the process finishes.

    sstouk - I read over and I see the idea, vaguely.  I will pursue coding today or tomorrow, and see how it goes.  Thanks so far...and if you think you understand that page well enough to offer a code snippet, that would be great.  :-)


    Author Comment

    I was able to make it work with some other help...thanks all.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This video discusses moving either the default database or any database to a new volume.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now