3

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.

2 Answers 2

1

Instead of system(), use qx:

print qx( "/full/path/to/another_script.pl" );
Sign up to request clarification or add additional context in comments.

Comments

0

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

5 Comments

That's interesting, would that technique of using "@>&1" capture both STDOUT and STDERR to my_log_file? And yes, I am using full paths to the main script within the crontab and to the sub-scripts from within the system() commands.
I added some additional details to my original post.
Yes - 2>&1 causes filehandle 2 (STDERR) to be redirected to filehandle 1 (STDOUT). Then the use of >> (for STDOUT) results in both STDOUT and STDERR output going to your logfile.
#/usr/bin/env perl <--- is perl in cron's PATH? Tho i expect if it works in the parent, it works in the subscript.
Other things to check: The return value of system() - it should be 0. Try using backticks $output = ``/full/path/to/another_script.pl``; print $output; (ok - I can't work out how to make this show a single backtick since that is the markup for "code") and trap errors. eval { system(" .. "); }; if ($@) { print "ERROR: $@\n"; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.