Running php scripts without crontab

I have a crontab with 200+ scheduled jobs and it is really difficult to remember when and with what parameters to work. And it's even more difficult to remember all the script names. And I will be in trouble if someone on the team deletes some of the jobs - the crontab will be copied but tu restrict it. I must be alarmed that something was removed or just commnented out.

So, I'm going to create one single cron job that will run all the other jobs that will be stored in the database and managed from the web interface.

I have one problem. This is how the site is crontab-like frequency - I mean something like this: * * 1 * *. When my only script runs, it should easily know that the wchich scripts should run. I need some advice because now all I can think of is to measure the time ()% on each run.

+3


source to share


1 answer


include()

all cron scripts in one file,

so when the cron job is started at this time, it will run all other script, but the timings will be the same.

Now to solve the time problem, create two database tables

1) Cron

id(int 11)              name(varchar)           flag(enum)
Auto Increament ID      name of php file        enabled/disabled

2) CronTimings

   id(int 11)    fk_cron_id (int 11)            time (time)               day(varchar)
   Auto incr     foreign key of first table     time to run the script    CSV days (i.e. Mon,Tue,Wed)

      

and a master table that stores the value of when the main cron was last executed

last_ran_time (time)
value of last time when 
cron has ran

      



Note. the second table can have multiple records with fk_cron_id

. (one script can have multiple timings)

now in your single cron file

$last_ran = "05:02:15";  // from the master table
$day = date('D');
$qry = "select * from cron c
        left join cronTimings ct on ct.fk_cron_id = c.id
        where ct.id in (select id from cronTimings where day like = '%".$day."%') and ct.time between '$last_ran' and '".date('H:i:s')."'";

//fetch data 
foreach($crons as $crn)
{
    include($crn['file'])
}

      

you can run this file every hour and a half or one hour, so if any cron needs to be running by this point, it will be retrieved with mysql between

...

let me know if any problem ...

+1


source







All Articles