Re: PHP run from cron fails occasionally

AllenJB <[email protected]> Sun, 13 Oct 2024 22:04:01 +0100
Newsgroups gmane.comp.php.general
Message-ID <[email protected]>
On 13/10/2024 21:20, Steve Matzura wrote:
>
> This is the strangest thing I've encountered yet. I thought the 
> original problem was the "-f" in the command, so I removed it. Still, 
> every couple days the command that runs the PHP script fails to 
> execute. Since no log is generated, I don't know what else to do to 
> diagnose this. If I run the script from the terminal, it always works, 
> creates the report and mails it. I've even looked in syslog for when 
> the cron job runs, and it indeed runs to completion with no errors. 
> Suggestions as to how to trace this one down most welcome.
>
Keep in mind that crons usually run under a different environment and 
possibly even a different user. For example the current working 
directory may not be what the code expects.

Another common cause of issues if the script executes external commands 
is that the PATH environment variable (which defines the paths checked 
when an executable is specified without a path) may not be set the same 
as when run under an interactive shell, so commands cannot be found.

A simple method of debugging scripts that run automatically is to log to 
a file, with timestamps. For debugging I usually log when the script 
starts and ends (as well as checking the script started and ended as 
expected, this is useful for monitoring how long it took to run). 
Monolog is probably the most commonly used library for logging and 
handles a lot of common cases for you, but you can also just use 
file_put_contents() (make sure to use FILE_APPEND mode). Ensure the 
directory where the logs are stored is writable when the script runs 
automatically.

(Using a library like monolog also allows you to take advantage of log 
levels - you can raise to DEBUG when you want to actively debug the 
script and then lower back to INFO or WARN the rest of the time, for 
example)

Set PHP's internal error handler to log to a file. You can do this on a 
per-script basis using ini_set() at the beginning of the script and the 
`error_log` and `log_errors` directives. Ensure that `error_reporting` 
is also set appropriately (I recommend E_ALL, especially when 
debugging). See 
https://www.php.net/manual/en/errorfunc.configuration.php#ini.error-log 
and ensure that the script can write to the specified path (when run 
automatically).

Monolog can also hook into PHP's custom error handling to append PHP 
errors to logs it creates.

External events may cause ("intermittent") issues. For example if a 
database server is restarted and severs all connections while the script 
is running. Particularly if the failure tends to happen around the same 
time, look at what else is happening on the server and other related 
servers at the same time.

On some hosting services, scripts that take longer than a specific time 
may be killed (even when not running as web requests).