I'm hoping I have missed a simple fix to this problem using Win32::Process, but I've done a lot of research and haven't found what I need.

I'm working on a "report engine". The main program queries the DB to retrieve a list of reports to be executed. With that information it builds the needed command line strings, which are then executed as threads using Win32::Process. The problem I'm having is not being able to capture the return from the command line for each thread and redirect it to a logfile. We really need to be able to capture any error messages to help us resolve issues faster and easier.

Here's a simple program file named ThreadLogTest.pl illustrating what I'm trying to accomplish with the redirection.

#!/usr/bin/perl use strict; use Win32; use Win32::Process; my $ProcessObj; my $perl_command = ''; print "Starting thread logging test\n"; $perl_command = 'C:\AS_v5.8.6_Perl\bin\perl.exe D:\FinancialReports_Pe +rlSource\PDS\output_test.pl >MyLog.txt 2>&1'; print "\n$perl_command\n"; Win32::Process::Create($ProcessObj,"C:\\AS_v5.8.6_Perl\\bin\\perl.exe" +,$perl_command,1,CREATE_NO_WINDOW,"."); print "Thread logging test completed\n"; exit;

The output_test.pl code called by ThreadLogTest.pl is

#!/usr/bin/perl print "\n\nThis is a test!\n\n"; exit;

The batch file that starts the whole process is

::Test thread logging @echo STARTED %time% Perl D:\FinancialReports_PerlSource\PDS\PDS_SURE\ThreadLogTest.pl >Thr +eadLogTest.txt 2>&1 @echo COMPLETED %time% @Pause

What I need is to have the output of output_test.pl written to the MyLog.txt file. That doesn't happen. I do get the following output in ThreadLogTest.txt

Starting thread logging test C:\AS_v5.8.6_Perl\bin\perl.exe D:\FinancialReports_PerlSource\PDS\outp +ut_test.pl >MyLog.txt 2>&1 Thread logging test completed

Nothing is written to MyLog.txt. If I copy the command line from ThreadLogTest.txt and execute it as a command then "This is a test" does get written to MyLog.txt, so I know the command line is properly formatted to redirect the output.

I really hope someone has used Win32::Process in a similar fashion, as it seems to be the perfect solution to what we want to accomplish if we can overcome this logging issue with the threads.

Thanks to everyone for your time!


In reply to Redirecting Output From Command Line in Threads by CKCJim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.