in reply to how to catch STDOUT from an external app, that never exits?

You don't say what else your script would be doing besides rotating the logfile for the external process, if anything?

If this is the only purpose of the script, then you might be better to pipe the output from the external process directly into the script via command line redirection and have the script re-write its input to the logfile, rotating the logs as required.

The simplest way to do this is to use the pl2bat command to convert your script into a .bat file as CMD.exe doesn't handle redirection from/to perl script directly. See the docs on pl2bat for further information on this.

Here is a short example script (with very crude date handling) to demonstrate the process.

#! perl -sw use strict; use POSIX qw[strftime]; my $lastdate = 0 ; while ( <STDIN> ) { my $now = strftime '%Y%m%d', localtime(); if ( $lastdate ne $now ) { close STDOUT; open STDOUT, '>', "log$now" or die $!; $|++; $lastdate = $now; } print STDOUT '>', $_; }

This is what it looks like once it has been pl2bat'd

@rem = '--*-Perl-*-- @echo off if "%OS%" == "Windows_NT" goto WinNT perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9 goto endofperl :WinNT perl -x -S %0 %* if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl if %errorlevel% == 9009 echo You do not have Perl in your PATH. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul goto endofperl @rem '; #! perl -sw #line 15 use strict; use POSIX qw[strftime]; print 'Starting...', $/; my $lastdate = 0 ; while ( <STDIN> ) { my $now = strftime '%Y%m%d', localtime(); if ( $lastdate ne $now ) { close STDOUT; open STDOUT, '>', "log$now" or die $!; $|++; $lastdate = $now; } print STDOUT $_; } __END__ :endofperl

You would use it like this (assuming you named the script logger.pl and did pl2bat logger.pl)

extcommand | logger

As is, it will create logfiles in the cwd with names like log20030409, with the name changing when the first line is written by the external command after midnight. It might get you going on something.


Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

Replies are listed 'Best First'.
Re: Re: how to cath STDOUT from an external app, that never exits?
by ThorstenHirsch (Novice) on Apr 09, 2003 at 12:21 UTC
    My mainscript does a few more things, but...

    ...if I understand you correctly, I can do the following:

    Mainscript:
    system("application.exe 2>&1 | logscript.bat &");
    which opens application.exe in the background and gives all STDOUT and STDERR to logscript.bat.

    And I have to write a pl2bat'ed logscript.bat, which only has to cope with its STDIN, the way you coded it. Right?
    I'll try this now.

    Thanks!

    Thorsten
      Hmmm...the thing with the logfile works. Thank you, my hero ;-)

      But I cannot get my application.exe starting up in the background! Both
      system("application.exe 2>&1 | logscript.bat &"); open3(NULL, ">&NULL", ">&NULL", "application.exe 2>&1 | logscript.bat +&");

      wait for application.exe to be finished or terminated.

      I'd be glad if someone would help me with this last piece of the puzzle.

      Thorsten