#!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 () { 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"; } #### #!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 () { 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); }