I have a file on the server that extract some data from pages, like a crawler. Ok, now, sometimes, the execution of the script takes 5 seconds, but sometimes takes even 1 minute or 2.
Setting a cron job for accessing the file at 2, 3, or 5 minute is not comfortable for me, because I need this crawler to run as fast as possible.
So my question is:
Can I set a cron job to run, let's say, every 5 minutes and set php to re-run the script again and again and again ?
More clear:
*/5 * * * * wget -O - site.com/index.php?cron >/dev/null 2>&1
index.php?cron
function cron_action()
{
//some action here
// Call again the function
cron_action();
}
cron_action();
As I don't understand very well how does cron job react at my script, I don't know either what will happen when, on another 5 minutes, the cron job will acces the url again.
I will be in a infinity loop ?
How you would do that ? I need some advices please. I really need to set the cron job run faster and, in my opinion, the functions from php must be recalled forever.
Setup cronjob:
* * * * * php /path/to/your/file.php --arg1=value1 --arg2=value2 >> /dev/null 2>&1
Your file.php
<?php
//test your args
var_dump($argv);
$minute = date('i',time());
//run every 10 minutes
if(intval($minute)%10==0){
//run your cron job code here
cron_action();
}
else{
//do other code
}
function cron_action()
{
//do stub
}
And this is how to use cronjob with laravel https://laravel.com/docs/5.3/scheduling, you will learn from that.
Possibly you can use the crontab to run it faster, but you can have a pid-lock(sort of) so that each time the crontab is called there can only one script running.