3

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.

8
  • include() all other scripts in the one cron ... but the timings of the script calling will be same... or more simple take timings and script name in your database and give them flag like 0 or 1, 0 for not to run and 1 for run... Commented Apr 17, 2015 at 12:08
  • May I suggest looking at supervisord Commented Apr 17, 2015 at 12:33
  • @AntonyD'Andrea, I don't really want to use other systems. it's not that complicated... Commented Apr 17, 2015 at 12:35
  • @NishantSolanki, didn't really get what you mean... e.g. I have weekly job scheduled. How master cronjob should detect that it is already week passed from the last run? Commented Apr 17, 2015 at 12:37
  • @ProstoTrader so all 200 cronjobs are weekly scheduled??? and if yes than on the same day or other days?? and yes single time a day or multiple times a day?? Commented Apr 17, 2015 at 12:38

1 Answer 1

1

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...

Sign up to request clarification or add additional context in comments.

4 Comments

great! that's what I'll do with some modifications - I want to measure perfomace of each script and save stats to tables
@ProstoTrader yep.. this script will need some modifications.. but it will give you a kickstart.. :)
If I want to run script every minute what should I do in yur architechture?
@ProstoTrader run the cronjob every one minute.. I dont think that will need to change my above architecture..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.