Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a PHP file is loaded via cron/command line

I need to determine whether the PHP file is being loaded via cron or command line within the code. How can I do this?

like image 794
Webnet Avatar asked Dec 06 '09 04:12

Webnet


People also ask

What is CLI PHP?

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.


1 Answers

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;
like image 79
gahooa Avatar answered Oct 12 '22 07:10

gahooa