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.
|
|---|
| 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 | |
by ThorstenHirsch (Novice) on Apr 09, 2003 at 13:53 UTC |