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.

In reply to Re: how to cath STDOUT from an external app, that never exits? by BrowserUk
in thread how to catch STDOUT from an external app, that never exits? by ThorstenHirsch

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.