I have some PHP scripts that run every minute via CRON. I don't want to receive emails every time they run correctly but I do want to receive an email if they throw an error (e.g. if they use too much memory).
To run the CRON:
php myscript.php >/dev/null
This should send an email only when the result of stderr is not null, i.e. when there is a standard error.
However, PHP errors don't create a stderr, instead they are included in the stdout so no email is sent.
I have tried setting display_errors to stderr as shown in this question but it's a different use case as the scripts are not run by CRON. I have tried the following which have not worked:
including this in the script itself doesn't work because a fatal errors mean the script doesn't run:
ini_set ('display_errors', 'stderr');
including this in an .htaccess file (note PHP FPM is used) didn't work - I'm not sure why:
php_flag display_errors stderr
Changing the php.ini file is not an option because I don't want the change to affect the whole site, just a few files.
So my question is - how can the PHP errors be sent to stderr so that I can receive them via email?
The php executable takes command line options - you can either specify which php.ini it should use, or set specific config options via the -d parameter.
https://www.php.net/manual/en/features.commandline.options.php
Related
I have a .php script that includes other php scripts and as final result a db is updated and a report file has been created.
if I manually call it from a browser all works fine.
I tried to call it from the crontab with the following syntax
/usr/local/bin/php /home/PATH/myscript.php
and at the planned time, it seems to stop at the first include(); and die.
I added a mail(); sending after each include();
If I call the file from the browser I receive all the 5 mails, when it's called from the crontab, I receive only the first email
Where am I doing wrong?
*edit:
I tried to call always the first script and I received all the mail. It seems the second script contains something that is stopping the code but ONLY is it's called by the cron
You can run a php script like this:
php -f script.php
If you want to use another php.ini (what often happens with console apps), use
php -c path/where/php.ini -f script.php
type
php --help
for more options.
Use this instead
php -q /home/PATH/myscript.php
As per this answer by St. Woland, the -q flag:
The -q flag suppresses HTTP header output.
I am trying to capture CLI errors written to stderr and email them. I have this part working.
The problem is, I need display_errors=true and error_log='file path'. When both these are used, php stops writing to stderr. Is there a way to make it write to stderr, stdout, and the log?
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, 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)
I have a PHP script that needs to run from command line. One of the function it calls is socket_create.
In php.ini, I have included the following in order to get that function working (see the comment on http://www.php.net/manual/en/sockets.installation.php):
extension=php_sockets.dll
How do I run the script from command line such that it doesn't complain that socket_create is an unknown function? Does PHP CLI actually respect what's in php.ini? I thought it was supposed to, but I do get errors when running it via CLI and no errors when running it via the browser.
UPDATE: by the way, I'm testing this out: https://github.com/nicokaiser/php-websocket. I assume that the server needs to be executed via command line.
A different php.ini file may apply when running php on the CLI rather than as a web server module. You appear to be using Windows, so I'm not sure where this file may be, but it could provide a clue; look for several php.ini files in your disk and see if one of them applies to CLI invocations.
Hi I'm running a PHP script via command line from a bash script. I am able to get the PHP errors appended to a single file with 2>> but I was wondering if I could send the errors to the standard PHP error_log file.
exec nohup php $PHP_SCRIPT_PATH 2>> $LOG_PATH & EPID=$!
Also when I do try to write to the php error_log file, I get write permission.
Thanks,
Steve
Even if you get your permission problem managed this will not work, because you write from two processes to the same file. This will most likely produce garbage in the error_log.
You have to prevent PHP writing to the error_log. Instead you have to configure PHP to write to STDERR. And than you can redirect the output to a single file.