当前位置: 动力学知识库 > 问答 > 编程问答 >

cron - Run php scripts without crontab

问题描述:

I have crontab with about 200+ scheduled jobs and it's really difficult to remember what when and with what parameters should run. And it's even harder to rememver all script names. And I'll be in trouble if someone from the team will delete some jobs - crontab is backed up, but tu restrore it I should be alarmed that something was deleted or simply commnented out.

So I'm thinking about creating ONE single cron job that will start all other jobs, that will be kept in in database and managed from web-interface.

I have one problems by now. It iss how to site crontab-like frequence - I mean something like this: * * 1 * *. When my single script starts, it should easily know wchich scripts should be run. I need an advise, because by now all I can think about - is to measure time()% on each run.

网友答案:

include() all the cron scripts in a single file,

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

Now to solve the time issue, 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 which stores the value when last time the master cron has ran

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

Note : second table can have multiple entries 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 half or one hour, so if any cron has to be run in between this time, it will be fetched using mysql between...

let me know if any issue...

分享给朋友:
您可能感兴趣的文章:
随机阅读: