todd412
todd412

Reputation: 1318

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.

Upvotes: 3

Views: 4252

Answers (2)

Pedro Silva
Pedro Silva

Reputation: 4700

Instead of system(), use qx:

print qx( "/full/path/to/another_script.pl" );

Upvotes: 1

pgregg
pgregg

Reputation: 34

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

Upvotes: 0

Related Questions