简体   繁体   中英

PHP error_log from CLI

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...

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, eg 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM