How do I redirect the output of Perl script executed from within another perl script to a file?
I am running a perl script via crontab and redircting its output to a file开发者_运维问答:
30 1 * * * /full/path/to/my_script.pl >> /full/path/to/my_log_file
Within my_script.pl, I'm executing several other perl scripts via the system() command:
#/usr/bin/env perl
system( "/full/path/to/another_script.pl" );
And within those scripts, I am using 'print' to write to STDOUT:
#/usr/bin/env perl
print "Standard output...\n";
It appears, however, that none of those child scripts' output is getting redirected to my_log_file- The only output I see there is that of the parent perl script. Am I missing something obvious? This is on a linux system.
Instead of system()
, use qx:
print qx( "/full/path/to/another_script.pl" );
Hmmm, if using system() then STDOUT should end up back in your log.
Do you have an example?
My immediate thoughts go towards the system() call isn't actually running the other scripts - do you use a full path to the script? Remember cron wont have the same $PATH that your shell has, so may not find the scripts you are trying to run unless they have the full path.
You could also capture STDERR in your log: 30 1 * * * /my_script.pl 2>&1 >> my_log_file
精彩评论