I need to determine whether the PHP file is being loaded via cron or command line within the code. How can I do this?
PHP's Command Line Interface (CLI) allows you to execute PHP scripts when logged in to your server through SSH. ServerPilot installs multiple versions of PHP on your server so there are multiple PHP executables available to run.
If you have control over the cron or command, have you considered passing a command-line argument, and reading it with $_SERVER['argv'][0]
?
* * * * * /usr/bin/php /path/to/script --cron
In the script:
<?php
if(isset($_SERVER['argv'][0]) and $_SERVER['argv'][0] == '--cron')
$I_AM_CRON = true;
else
$I_AM_CRON = false;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With