A
A
artem782016-12-14 02:29:49
linux
artem78, 2016-12-14 02:29:49

How to determine in a php script that it was launched by Cron?

How to determine in a php script that it was launched by Cron?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene Volf, 2016-12-14
@Wolfnsex

Pass it an input parameter, such as -cron .

T
theg4sh, 2016-12-16
@theg4sh

As far as I know, when running "manually" the $_SERVER['argc'] and $_SERVER['argv'] variables appear, and other user environment variables also appear.
Hence:

if (isset($_SERVER['argc']) && isset($_SERVER['argv'])) {
    define('SELFSERVICE', 1);
}

if (defined('SELFSERVICE')) {
    echo "I'm a service";
} else {
    echo "I'm a web-dispatcher";
}

Well, or run the script via http request
curl "http://name.site/script.php?cron=1"

S
SlavikF, 2016-12-14
@SlavikF

Look at the output
of phpinfo();
for the case when it is opened in the crown and for the case when it is opened through the web server.
And choose for yourself the parameter(s) by which you will determine
For example, like this:
$isCron = !isset($_ENV['SSH_CLIENT']);
But it may depend on the system

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question