Here is a version for ActivePerl:
#!perl -w use Getopt::Std; use POSIX; use strict; my %opt; getopts("c", \%opt); if ($opt{c}) { dochild(); } else { # MSWin version use Win32::Process; my $ProcessObj; Win32::Process::Create($ProcessObj, 'C:\Perl\bin\wperl.exe', "perl $0 -c", 0, NORMAL_PRIORITY_CLASS|DETACHED_PROCESS, ".")|| die ErrorReport(); } sub bgerror { # Here I am just dying, but you may want to do something # else since this is a background process and the # errors are not going anywhere die $_[0]; } sub dochild { my $tomorrow = 0; my $SCRIPTLOG; open APP, "cat testfile |" or bgerror("Cannot execute app.exe: $!\n" +); while (<APP>) { if (time() >= $tomorrow) { my $now = time(); $SCRIPTLOG = logname($now); $tomorrow = gettomorrow($now); open LOG, ">", $SCRIPTLOG or bgerror("Cannot open $SCRIPTLOG: $!\n"); select(LOG); $| = 1; } print $_; } bgerror("App.exe exited\n"); } sub logname { my ($sec,$min,$hour,$mday,$mon,$year) = localtime($_[0]); sprintf "log%d%02d%02d", $year+1900, $mon+1, $mday; } sub gettomorrow { my ($sec,$min,$hour,$mday,$mon,$year) = localtime($_[0]); my $tomorrow = 25*60*60 + POSIX::mktime(0 ,0, 0, $mday, $mon, $year,0 +,0,-1); # 25 hours from midnight today (25 because of DST shift) ($sec,$min,$hour,$mday,$mon,$year) = localtime($tomorrow); return POSIX::mktime(0 ,0, 0, $mday, $mon, $year,0,0,-1); } sub ErrorReport{ Win32::FormatMessage( Win32::GetLastError() ) . "\n"; }
and here is a version for Unix or for cygwin Perl (cygwin is Unix-like environment for Win32):
#!perl -w use Getopt::Std; use POSIX; use strict; # Unix/cygwin version my $child = fork(); if (not defined($child)) { die "Fork unsuccessful: $!\n"; } if ($child == 0) { dochild(); } sub bgerror { # Here I am just dying, but you may want to do something # else since this is a background process and the # errors are not going anywhere die $_[0]; } sub dochild { my $tomorrow = 0; my $SCRIPTLOG; open APP, "cat testfile |" or bgerror("Cannot execute app.exe: $!\n" +); while (<APP>) { if (time() >= $tomorrow) { my $now = time(); $SCRIPTLOG = logname($now); $tomorrow = gettomorrow($now); open LOG, ">", $SCRIPTLOG or bgerror("Cannot open $SCRIPTLOG: $!\n"); select(LOG); $| = 1; } print $_; } bgerror("App.exe exited\n"); } sub logname { my ($sec,$min,$hour,$mday,$mon,$year) = localtime($_[0]); sprintf "log%d%02d%02d", $year+1900, $mon+1, $mday; } sub gettomorrow { my ($sec,$min,$hour,$mday,$mon,$year) = localtime($_[0]); my $tomorrow = 25*60*60 + POSIX::mktime(0 ,0, 0, $mday, $mon, $year,0 +,0,-1); # 25 hours from midnight today (25 because of DST shift) ($sec,$min,$hour,$mday,$mon,$year) = localtime($tomorrow); return POSIX::mktime(0 ,0, 0, $mday, $mon, $year,0,0,-1); }

In reply to Re: how to cath STDOUT from an external app, that never exits? by Thelonius
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.