Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

This question already has an answer here:

Good morning, I have into my ubunut server a script that check into a database some value and if the condition is true send some email. If I launch into the browser the script like:

http://www.my_site.com/reports/generate.php

This works, mail is arrived.

If into my crontab I write this line into the file /etc/crontab:

0  12  * * * root php /var/www/reports/generate.php

This doesn't work, mail isn't arrived and the value are the same. I have check the path and is correct, I give 777 permission to this file, but doesn't work in cron.

Can someone explain me why in cron deosn't work?

This script take about 5 minutes to end.

This is the syslog.log rfer to the script

Jun 17 12:00:03 site /USR/SBIN/CRON[5352]: (root) CMD (   php /var/www/reports/generate.php)
Jun 17 12:00:07 site postfix/pickup[2834]: 10546128001: uid=0 from=<root>
Jun 17 12:00:07 site postfix/cleanup[5381]: 10546128001: message-id=<20130617100007.10546128001@site.localdomain>
Jun 17 12:00:07 site postfix/qmgr[3259]: 10546128001: from=<root@site.localdomain>, size=941, nrcpt=1 (queue active)
Jun 17 12:00:08 site postfix/local[5384]: warning: dict_nis_init: NIS domain name not set - NIS lookups disabled
Jun 17 12:00:08 site postfix/local[5384]: 10546128001: to=<vpsimple@site.localdomain>, orig_to=<root>, relay=local, delay=2, $
Jun 17 12:00:08 site postfix/cleanup[5381]: 438CD128002: message-id=<20130617100008.438CD128002@site.localdomain>
Jun 17 12:00:08 site postfix/qmgr[3259]: 438CD128002: from=<>, size=2759, nrcpt=1 (queue active)
Jun 17 12:00:08 site postfix/bounce[5386]: 10546128001: sender non-delivery notification: 438CD128002
Jun 17 12:00:08 site postfix/qmgr[3259]: 10546128001: removed
Jun 17 12:00:08 site postfix/local[5384]: 438CD128002: to=<xxx@site.localdomain>, orig_to=<root@site.localdomain>, re$
Jun 17 12:00:08 site postfix/qmgr[3259]: 438CD128002: removed
Jun 17 12:03:24 site kernel: Shorewall:net2fw:DROP:IN=eth0 OUT= MAC=00:16:3e:70:a5:ad:00:30:48:56:76:d3:08:00 SRC=122.228.129.25$
Jun 17 12:09:03 site /USR/SBIN/CRON[6104]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/l$
Jun 17 12:13:13 site kernel: Shorewall:net2fw:DROP:IN=eth0 OUT= MAC=00:16:3e:70:a5:ad:00:30:48:56:76:d3:08:00 SRC=61.188.37.237 $
Jun 17 12:15:32 site postfix/pickup[2834]: 2B3B1128001: uid=33 from=<www-data>

-------------------------EDIT---------------------
I have change the file crontab in this mode:

0 13 * * * root php /usr/bin/php -f /var/www/reports/generate.php 1>/dev/null 2>&1

and not works, this is the new syslog.log

Jun 17 13:00:03 site /USR/SBIN/CRON[9886]: (root) CMD (   php /usr/bin/php -f /var/www/reports/generate.php $
Jun 17 13:01:19 site kernel: Shorewall:net2fw:DROP:IN=eth0 OUT= MAC=00:16:3e:70:a5:ad:00:30:48:56:76:d3:08:00 SRC=122.141.177.11$
Jun 17 13:09:02 site /USR/SBIN/CRON[10624]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/$
Jun 17 13:17:02 site /USR/SBIN/CRON[11246]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly 1>/dev/null 2>&1)
share|improve this question

marked as duplicate by Iain, Stephane, mdpc, Falcon Momot, rnxrx Jun 17 '13 at 19:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Work your way through serverfault.com/questions/449651/… it will almost certainly help you. –  Iain Jun 17 '13 at 9:17
    
0 13 * * * root php /usr/bin/php: Remove the 'php'!!! –  AndrewQ Jun 17 '13 at 9:40
    
Tried but no mail to me... @AndrewQ –  WebDesigner Jun 17 '13 at 10:32
    
Did you got the right php page processed? –  AndrewQ Jun 17 '13 at 11:05

3 Answers 3

up vote 1 down vote accepted

You must write the full path of the php executable (maybe /usr/bin/php):

0 12 * * * root /usr/bin/php -f /var/www/reports/generate.php

The -f parameter is not mandatory, but helps the php parser.

EDIT: The "root" user is mandatory in the files you write in /etc/cron.{d, hourly, daily, weekly, monthly} directory, but if you are using the "old" crontab (crontab -e), the user is always root, and is not required. Examine the "/var/log/cron" "/var/log/syslog" file to discover the real error.

share|improve this answer
    
no log named cron in /var/log –  WebDesigner Jun 17 '13 at 8:51
    
Added the syslog.log to the question –  WebDesigner Jun 17 '13 at 9:12
    
You don't get email because of a bounce. Check your Postfix configuration, and the From: and To: fields. –  AndrewQ Jun 17 '13 at 9:29
    
If I run the same script into the browser it works sender and receiver are ok –  WebDesigner Jun 17 '13 at 9:30
    
edit question with new log file –  WebDesigner Jun 17 '13 at 9:33

FYI: If you do not want to run PHP in crontab, you can always point to PHP inside the script by adding:

#!/usr/bin/php -q

To the first line of the script. This advices that the script must be run under a PHP and removes the need of adding PHP command to the crontab before the actual script.

Above is actually a good solution also when you need to list/observe processes going on your system. If you use PHP command in front of the script there is a danger that process-listing shows only it and not only the name of the script.

Also, if you want to spare yourself from receiving executing data from the script to mailbox (usually to roots), you may want to add > /dev/null 2>&1 after the script.

share|improve this answer
    
Added the syslog.log to the question –  WebDesigner Jun 17 '13 at 9:12

In your crontab line change the line from

0  12  * * * root php /var/www/reports/generate.php

to

0  12  * * * root /usr/bin/php /var/www/reports/generate.php

Maybe if you don't define the php's path in the crontab file the script couldn't run. To check if this one could be the problem before that the script run, try to use tail -f /var/log/syslog . The -f option write on screen everything in real time.

So you can check what should be your problem. On this page there are the causes about why a cron job doesn't run. Cron doesn't run

share|improve this answer
    
edit question with new log file –  WebDesigner Jun 17 '13 at 9:34

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