Please tell me what am I doing wrong, why doesn't this run:

 3 * * * * /usr/bin/php -q /var/www/CRON/hourly.php > /var/www/logPHP.log

logPHP.log is made, but is empty when this is run. If I replace the 3 with an *, the script starts running but never ends.

When I copy this in Terminal:

/usr/bin/php -q /var/www/CRON/hourly.php > /var/www/logPHP.log

Everything runs smoothly as it is supposed to.

Ubuntu 10.04 LTS,

PHP 5.3.2-1ubuntu4.9 with Suhosin-Patch (cli)
  • what about creating .sh with that command and running that script via cron? – Marek Sebera Aug 28 '11 at 9:13
  • What do you mean by "doesn't run"? The script should start 3mins after every full hour. You are sure you didn't mixed it up with "every 3min"? – KingCrunch Aug 28 '11 at 9:14
  • Please don't hate me for bringing this up but have you checked that you may have some illegal linebreaks or space characters in the cron file that my happend during copying / editing the file? – Cadoc Aug 28 '11 at 9:14
  • Didn't copy the file, edited with nano. (crontab -e) @KingCrunch - I know that it won't run every 3 minutes. I change the minute value 1 minute before the next minute to test. – Methemer Aug 28 '11 at 9:55
  • 1
    @Methemer Perhaps you should share the php code too, there may be something inside that is dependent on the environment in which it runs that is not present when executed by cron. Or at least make sure that you error reporting on so any potential error is printed to log. – CodeReaper Aug 28 '11 at 10:15

If you use crontab -e to create cron entries ignore the missing user part.

Check the syntax for cron entries, you are missing the "user", e.g. :

3 * * * * some_user_here /usr/bin/php -q /var/www/CRON/hourly.php > /var/www/logPHP.log

Check that some_user_here as writing permissions to the log file by verifying that there are no errors when you do this:

su -m some_user_here
cd /var/www/
touch logPHP.log

Check that php executable is present where you specified by confirming that /usr/bin/php is return when you do this:

whereis php

Did you use a guide when setting up your cron entry, e.g. https://help.ubuntu.com/community/CronHowto ?

  • Added the user, didn't help. – Methemer Aug 28 '11 at 9:55
  • 2
    You need to put in the user only if you're putting files into /etc/cron.d. If you're setting up the crontab for the individual user (i.e., running "crontab -e", then the format used by Methemer is correct). – cjc Aug 29 '11 at 11:07
  • Where did you get this syntax from? Cron has been around for a very long time, only /etc/crontab uses this syntax - user crontab files do not have a user field. – symcbean Aug 29 '11 at 11:51
  • Methemer did not say he used crontab -e and /etc/crontab is still used (at least by me). crontab -e hides away what cron jobs that are being run and using /etc/crontab allows me to have one configuration where all cron jobs are listed and where I can specify which user should run what... and the syntax came from here help.ubuntu.com/community/CronHowto#Tips – CodeReaper Aug 29 '11 at 15:13

You are logging standard output (STDOUT) to a logfile, but you are ignoring standard error (STDERR). It is almost always a good idea to send STDERR to a logfile. If you ignore the errors, you may be missing out on critical problems.

Modify your cronjob to redirect STDERR to the same logfile, using 2>&1:

3 * * * * some_user_here /usr/bin/php -q /var/www/CRON/hourly.php > /var/www/logPHP.log 2>&1

Or alternatively, send the errors to a second logfile:

3 * * * * some_user_here /usr/bin/php -q /var/www/CRON/hourly.php > /var/www/logPHP.log 2> /var/www/logPHP.err

Then, allow the script to run again, and check /var/www/logPHP.log for errors. At that point, you will probably be able to solve the problem on your own. If you still need help, you can update your answer here with the output of the error log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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