2

Users of my application need to be able to schedule certain task to run at certain times (e.g. once only, every every minute, every hour, etc.). My plan is to have a cron run a script every minute to check the application to see if it has tasks to execute. If so, then execute the tasks.

Questions:

  1. Is the running of cron every minute a good idea?
  2. How do I model in the database intervals like cron does (e.g. every minute, ever 5th minute of every hour, etc.)?

I'm using LAMP.

StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441
  • I don't know if you're using Java or not, but you can take a look at Quartz Scheduler. http://www.quartz-scheduler.org/ – Jared Farrish Jul 26 '11 at 23:55
  • This might also be useful to review (scroll down to the examples): http://www.quartz-scheduler.org/docs/tutorials/crontrigger.html – Jared Farrish Jul 26 '11 at 23:57
  • You might look at this as a starting point: http://www.phpjobscheduler.co.uk/ – Jared Farrish Jul 27 '11 at 00:49
  • Also, as far as storing your cron schedule, you can just store a cron expression. They're fairly easy to understand. You'd have to right a webform to allow your users to create an expression, but I don't imagine that would have to be all that difficult (depending on how complex you wanted it to be). http://download.oracle.com/docs/cd/E14003_01/doc/doc.1014/e12030/cron_expressions.htm – Jared Farrish Jul 27 '11 at 01:14

2 Answers2

1

Or, rather than doing any, you know, real work, simply create an interface for the users, and then publish entries in cron! Rather than having cron call you every minute, have it call scripts as directed by the users. When they add or change jobs, rewrite the crontab.

No big deal.

In unix, cron allows each user (unix login that is) to have their own crontab, so you can have one dedicated to your app, don't have to use the root crontab for this.

Will Hartung
  • 115,893
  • 19
  • 128
  • 203
  • I suppose this is possible. It means that each account user would need to have an associated unix user. That might be a bit complex??? – StackOverflowNewbie Jul 27 '11 at 00:16
  • No, my point was simply that you have a user associated with your application, and use that crontab -- there's no reason for there to be a unix user for each app user. – Will Hartung Jul 27 '11 at 00:22
  • I think this idea might be workable. I'll give it some more thought. Have you done anything like this? – StackOverflowNewbie Jul 27 '11 at 01:00
  • No, I was using Java, and wrote my own scheduler. It's was a bit of work, and cron really wasn't appropriate for it anyway. But for yours, it's pretty straightforward. Assuming you can convert the job that the user wants to run in to a shell script, you can capture the job settings (type, input data, schedule, etc.) in to an internal job table in the db, then use that table to create a script (perhaps named after the primary key), and then simply dump out the crontab, and then use 'crontab -u' to reload it. – Will Hartung Jul 27 '11 at 02:19
  • I just realized this might be hard to implement. I need to provide the ability to let the user modify these tasks (e.g. change the frequency, etc.). Also, a start and stop date needs to be specified for each tasks; cron doesn't support that, right? I think I will need a database solution instead of trying to manipulate the cron file. What do you think? – StackOverflowNewbie Jul 27 '11 at 23:01
  • When a user requests a change, you simply update the crontab. As for the start and stop date, add a job to run at 11:59pm each night to regenerate the crontab for "tomorrow". Once you get the "generate the crontab and scripts" code written, you'll be able to do anything you want within reason. But eventually it may well get too complicated and calling the "run every minute" implementation may be easier. But it will have downsides as well. – Will Hartung Jul 28 '11 at 00:03
  • I guess I am back to my original question: how should I model intervals in the database? – StackOverflowNewbie Jul 28 '11 at 00:11
0

Do you mean that you have a series of user-defined jobs that need executed in user-defined intervals, and you'd like to have cron facilitate the processing of those jobs? If so, you'd want to have a database with at least 2 fields: JOB, OFTEN

where OFTEN is how often they'd like the job to run, using syntax similar to CRON.

you'd then need to write a script (in python, ruby, or some similar language) to parse that data. this script would be what runs every 1 minute via your actual cron.

take a look at this StackOverflow question, and this StackOverflow question, regarding how to parse crontab data via python.

Community
  • 1
  • 1
Patrick Perini
  • 22,555
  • 12
  • 59
  • 88