Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading