1

I have a script That should execute a piece of code only if the script is run from a cron job, but i do not know the condition "if the script is performed by cronjob".

2 Answers2

1

Simply use this question's answer: Using CRON jobs to visit url?

However, supply the URL with a parameter that indicates it is being from the CRON Job.

* * * * * wget -O - http://yoursite.com/script.php?fromcron=1 >/dev/null 2>&1

script.php

if(!isset($_GET['fromcron']) || $_GET['fromcron'] !== 1) {
    exit(); 
}

// ...
Community
  • 1
  • 1
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
0

If you have 2 types of requests, from server and from remote, you can determine the different between them by variable $_SERVER.

If you run a php file from php command, $_SERVER['REMOTE_ADDR'] will not be set. Therefore, you can use:

if( !isset($_SERVER['REMOTE_ADDR'] ) {
    // The script is run by php command
}

However, if your 2 types of requests are run by php command, you would better use 2 php files, first is the normal one, while the second one will include the first one and also run specific script that will be run by cronjob.

spicydog
  • 1,644
  • 1
  • 17
  • 32