Perl not executing shell commands when run from cron

I have a PERL script which calls a PHP script from the command line and sends the output via e-mail. This script works fine when executed from the command line, but fails when executed via cron. I have tried executing the script from my own crontab, as well as the root cron, same result.

This is the script:

#!/usr/bin/perl
my $file = "/app/testing/testfile.php";
my $output = `php $file`;
#Sending e-mail here

And this is the PHP script that it is calling:

fopen('/app/testing/log.txt', 'w');
fwrite('hi');
echo 'hi';

When run from the command line, I get an e-mail with the word "hi" and the file has the word hi. Run from cron, I get a blank e-mail and the file is not modified.

>Solution :

Provide full path to php:

my $output = `/path/to/php $file`;

This is because cron uses a rather simplified environment, with a minimalistic $PATH.

Alternatively, source your .*rc file in cron, but that may be an overkill:

03 */1 * * * source ~/.zshrc &> /dev/null ; /path/to/my_script.pl

Leave a Reply