Create a random cron minute for a specific date and time

I try to do random cron jobs where I select the date and hour of the year of the month, but the minute is randomized.

My first attempt was to run cron every min, and then compare a random date with the current date:

I inserted a random date into the database column fake_timein format 2014-10-26 17 rand(0,59). On the php page where I run cron every min:

if($row["fake_time"] == date("Y-m-d H i")){
    //do stuff
}

And it worked perfectly. But then I found out that I cannot start cron every min, because my host (host agent) will not let me! Do you have ideas on how I can do this in any other way?


Or should I just set it up at https://www.easycron.com/ instead?

+4
source share
2 answers

I think that you are limited by the number of cron jobs that you can run in one day, the IIRC hoster has a daily limit for the basic plan. To get around this limitation, IMO, you have two options:

  • Go in sleepfor 60 seconds

    Basically, run the cron task at the required hour every day and check if it is not True, then go to sleep for 60 seconds.

    if($row["fake_time"] == date("Y-m-d H i")){
        //do stuff
    } else {
        sleep(60);
    }
    

    , cron, . , cron , , , 3600 , 60 .

    set_time_limit .

  • easycron

    cron , , easycron.

    script php, script.php, cronjob, script. :

    wget your.domain.com/script.php
    
+1

, php script ( ).

if(rand(1, 5) == 1){
// do your staff
}

, script , rand(1,3)

0

All Articles