0

I have a php mailer script that needs to run every hour via cron job. Basically it collects information from a database attaches this in the phpmailer via html and sends it out.

Via the web service if i browse to the mailer it runs perfectly and i receive the mail. However when doing this via cron the mail does not send out.

I have currently set the cron to run every 5 minutes just to test and touch a tmp file just to know it has run:

*/5 * * * * /var/www/Mailer/311-daily-production-3shifts-mailer.php; touch /tmp/mailerhasrun

anyone know what the issue can be? i am logged in via the root user on my pi.

EDIT

If i now run the script directly from the command line i get the following error, however it is still working from the web service

root@raspberrypi:~# php /var/www/Mailer/311-daily-production-3shifts-mailer.php
PHP Warning:  include(./scripts/311-daily-production-3shifts.php): failed to open stream: No such file or directory in /var/www/Mailer/311-daily-production-3shifts-mailer.php on line 10
PHP Warning:  include(): Failed opening './scripts/311-daily-production-3shifts.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/Mailer/311-daily-production-3shifts-mailer.php on line 10
PHP Warning:  require(./library/PHPMailerAutoload.php): failed to open stream: No such file or directory in /var/www/Mailer/311-daily-production-3shifts-mailer.php on line 14
PHP Fatal error:  require(): Failed opening required './library/PHPMailerAutoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/Mailer/311-daily-production-3shifts-mailer.php on line 14

EDIT:

I have fixed my php errors and when i run /usr/bin/php /var/www/Mailer/311-daily-production-3shifts-mailer.php from command line it sends the mail as required. However the cron is still not working.

3
  • also if i run the script from terminal / command line it runs perfectly :: php /var/www/Mailer/311-blah-blah.php Commented Jul 2, 2014 at 6:33
  • errors you mention in updated question are PHP errors, you should check your code. Commented Jul 2, 2014 at 7:04
  • fixed coding errors. script runs from command line but not cron Commented Jul 2, 2014 at 7:10

1 Answer 1

1

Use correct shebang[1] line in your script. Your php-cli executable path may differ, please find your your executable before editing.

#!/usr/bin/php
<?php
  // your script here;
?>

to use crontab in correct way, you should give full path to run interpreter or set PATH variable before cron lines. Also, you should use && instead ; .

( ; : run second command regardless of first command output. && : run second command if first command ran successfully. )

In your case it should like below.

*/5 * * * * /usr/bin/php /var/www/Mailer/311-daily-production-3shifts-mailer.php && touch /tmp/mailerhasrun

[1] http://en.wikipedia.org/wiki/Shebang_(Unix)

3
  • Thanks for the prompt reply. I tried your suggestion as above but still not working. when i try running the script via command line using :: php /var/www/Mailer/311-blah-blah.php it runs perfectly but if i do /usr/bin/php /var/www/Mailer/311-blah-blah.php i get a couple include errors also the cron is not running Commented Jul 2, 2014 at 6:49
  • @tottiemonster please update your question with errors which you have instead of comment. Commented Jul 2, 2014 at 6:52
  • Edited original question Commented Jul 2, 2014 at 6:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.