1

I'm using crontab to call a php script.

In this script there is

error_log('test');

When the script is executed from http or direct command line like

php -f script.php

Everything is fine, my error is log.

But when called from cron it's not working.

Here is my cron

* * * * * -u www-data /full_path_to/php -f /full_path_to/script.php

Here is what I tried :

  • error_log with arguments :

    error_log('test', 3, '/full_path_to/error.log');
    
  • changing error reporting :

    error_reporting(E_ALL);
    ini_set('display_errors','On');
    ini_set('error_log', '/full_path_to/error.log');
    
  • cron call ending with > /full_path_to/error.log 2>&1 (don't know if useful)

For http the error_log path is set from htaccess. I'm lost with php cli...

I can see the cron execution working every minute (syslog), so it should be a PHP config problem ?

Thanks a lot if you can help.

Edit : Cron is executed with "-u www-data"

Here is the call I see in syslog :

CRON[13921]: (www-data) CMD (-u www-data /usr/bin/php -f /fullpath/script.php > /fullpath/error.log 2>&1)
FLX
  • 2,626
  • 4
  • 26
  • 57
  • 1
    Does the script **only** contain that log line? Or is there more context that you're not showing us? Because in itself, this should work. So that tells me that there's most likely missing context . Most common pitfall is using includes with relative paths. – Oldskool Jan 21 '16 at 11:16
  • Yes, right know there are 4 lines : error reporting lines and error_log. Creating a file is also not working (I tried fopen/fwrite and also file_put_contents) – FLX Jan 21 '16 at 11:20
  • Sounds like a permissions problem then. Try running the cron under a user that has access to the required paths (or as root, but that should be a last resort). – Oldskool Jan 21 '16 at 11:23
  • The cron is running as www-data. This AFAIK the same user running apache. Same user, same group... I'll try to set chmod to 777 – FLX Jan 21 '16 at 11:25
  • Don't do that, chmod 777 is always a bad idea. *Especially* when it regards web accessible files. Also see [How will a server become vulnerable with chmod 777?](http://stackoverflow.com/questions/11271596/how-will-a-server-become-vulnerable-with-chmod-777) – Oldskool Jan 21 '16 at 11:27
  • It changed nothing :( I if write random error in the script like ZZZ, nothing appears in syslog – FLX Jan 21 '16 at 11:31
  • Does the `cron` environment have the same `include path` settings as the `direct command line`? i.e. `path variables` etc. Display both shell environment settings from inside the script ($_SERVER etc.) - I suspect they are different. Make the cron job settings match and it should work? – Ryan Vincent Feb 05 '16 at 18:09
  • @RyanVincent interesting it could be that – FLX Feb 08 '16 at 08:40
  • I have answered a similar [question](http://stackoverflow.com/questions/6322682/cron-not-passing-params-to-php-script) Please [check my answer](http://stackoverflow.com/a/42533773/6670698) – Shammi Shailaj Mar 01 '17 at 14:15

3 Answers3

1

I was facing the same problem but was able to fix it after reading the manual entry for php.

Initially I had something set like:

/usr/bin/php -f /path/to/php/script.php -c 1426 >> /tmp/script.php.log 2>&1

I was able to fix it by changing the line to:

/usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1

As per the manual entry the syntax is:

php [options] [ -f ] file [[--] args...]

Also,

args...        Arguments passed to script. Use '--' args when first argument starts with '-' or script is read from stdin

Going by that, my cron command becomes:

/usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1

and it works!

Shammi Shailaj
  • 445
  • 5
  • 14
0

You have to be aware that there are user specific crontabs and a system wide crontab.

When you are logged in as user foo and type crontab -e in console this will allow you to edit your own user specific crontab. All defined cron tasks will be executed as user foo. This is even true for user root. AFAIK this way you simply can not change the user under which a cron task will be run.

Quite different is the file /etc/crontab. This is the system wide crontab. Within that file you can change the user of a cron task like this:

* * * * * www-data /full_path_to/php -f /full_path_to/script.php
maxhb
  • 8,554
  • 9
  • 29
  • 53
0

I had in fact two problems :

1) My cron was executed with php.ini for php-cli. I used the -c flag to load the good one.

2) I was relying on $_SERVER to declare important constants using variables that do not exist in cli-mode. Now if these variables are not set I parse commande line vars with getopt()

FLX
  • 2,626
  • 4
  • 26
  • 57