2

I'm calling a php script using CRON.

The script use a lot the error_log function :

error_log('My error');

Seems like it's not working from CLI.

I can make it work by using more arguments :

error_log('My error', 3, '/fullpath/to/my/log');

But I would like to avoid modifying everything (my script include a lot of other scripts).

As far as I understand, PHP is using a different php.ini when called from command line.

Is there a way to force it to use the normal php.ini ?

I need my cron to execute this script in the exact same environment that from the web.

Are there problems I should be aware of ? Others differences that could break my code ?

Edit :

I found a way to tell php which php.ini file to use (-c):

/path/to/php5 -c /path/to/php.ini /path/to/script.php

But it's not working. In my script shell_exec('php --ini') is still showing cli/php.ini...

1 Answer 1

3

If you need your cron to execute this script in the exact same environment that from the web, just call it from the web:

  • setup a virtual host (local only, e.g. on port 4242, locked with iptables)
  • run cron as curl http://localhost:4242/script.php

It will run the script as a webserver user, using all environment variables, configs, and logs.

To solve the exact problem with logging, just redirect stderr to a file:

/path/to/php5  /path/to/script.php 2> /fullpath/to/my/log

The lst part: shell_exec('php --ini') shows default cli/php.ini because you start new process with default config. To show custom config either specify it in the command line

shell_exec('php --ini -c /path/to/php.ini')

or show info for current process:

phpinfo(INFO_GENERAL)
Sign up to request clarification or add additional context in comments.

3 Comments

The script is outside root path to avoid it being called by users. But if I can't resolve this i'll follow your advice. Everything should be fine with just php -c...
If you give up with stderr redirection, read the bullet points in the answer how to setup it properly to avoid being called by users.
Oh, I can see now the stupidity of what I did. Cron called with good php.ini, but inside of it shell exec have no -c flag... Took me so much time to see the light ^^

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.