Fairly new to Perl.
I have a Perl script on a Linux machine, which has own logfile. Logfile name can change, dependent on data the script is working on (date, filename, datatype, etc.)
The script at some pionts is calling a native executable with system() call, which gives some information out to STDOUT and STDERR - few tens to few hundreds lines over many minutes. After the executable is done, the script continues and logs some other info to the logfile.
Until now the script only logged its own output, without the native executables output, which I want to log in same files as the Perl script logs to. Tried it with following two methods:
#some other code
@array_executable_and_parameters = qw/echo foo/ ;
open $log_fh, '>>', 'log/logfile1.txt';
*STDOUT = $log_fh;
*STDERR = $log_fh;
open(LOGFILEHANDLE, ">>$logfilename" );
*STDOUT = LOGFILEHANDLE;
*STDERR = LOGFILEHANDLE;
print LOGFILEHANDLE "Somethinglogged\n";
*STDOUT is only affecting the Perl-internal
STDOUT scalar's binding. The proper way to redirect standard output on the system level is something like
open (STDOUT, '>&', $log_fh) or die "$0: could not: $!";
You should similarly report errors from your other system calls which could fail (and
use strict and etc).
cron runs your job in your home directory, so if the path
$HOME/log does not exist, the script will fail to open the log file handle (silently, because you are not logging