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".
Asked
Active
Viewed 397 times
1
-
before execute a script (in header) send some query string in click event.. if query string has come its not executed in cron job or else its executed in cronjob – Arun Feb 16 '15 at 13:25
-
1Why don't you just pass a parameter when running by cron job? – Henrik Feb 16 '15 at 13:25
-
My hosting does not allow me to pass parameters, do not know the motivation. – Giuseppe Grieco Feb 16 '15 at 13:27
-
create a script that passes the parameters, call that from cron... – Karoly Horvath Feb 16 '15 at 13:34
2 Answers
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();
}
// ...
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
-
@GiuseppeGrieco It would be nice if you make this as the answer. Thank. – spicydog Nov 16 '16 at 13:06