Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm crashing my head over a design decision, and I'm trying to get some more inspiration/hints/tips/.. from other programmers.

What is the goal:

I want to run one cron task (php script) every minute.

In this script I should check a database full of schedules (same syntax as cron jobs) to see if an action should run right now. (An action can be sending out an email, sending out text-message, ..)

I don't think it's possible with sql only, so the best is to retrieve all schedules, and check in PHP which one of them should be executed at the given moment.

I'll also store the "startFrom" date, to say when this schedule should be triggered first.

Any suggestions on this one?

share|improve this question
It's not quite clear in your question: Do you want to get all the schedules every minute, or do you want to do this ones and then check periodically what needs to be executed? –  mri Feb 26 '13 at 15:04
Schedules should be defined in the db, and a cron that runs every minute should check what script/action should be executed. Compare it with a cron for every action, but I don't (can't) create a new cron for every action. –  jverdeyen Feb 26 '13 at 20:10
add comment

closed as not constructive by gnat, MichaelT, ChrisF Feb 26 '13 at 19:50

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

The common approach I've seen is to have one php script that is executed by crontab that populates (in your case, from a MySQL db) additional tasks to be fired from exec. The hitch with this approach is that without doing some pretty heavy calculation on timestamps manually, it's hard to make this work practically when you have granular scheduling requirements for each task (they can't all be run together at once once per day, for example).

If I were in your shoes and I was looking for a lightweight approach, I'd look into alternatives for generating my crontab rather than keeping the scheduling in PHP proper. You could create a php script that outputs to stdout that could be used to generate a valid crontab with the proper schedules, perhaps fetched from a database like you describe. Obviously the script would have to be executed such that it had the right to edit the crontab directly, but then at least you'd be keeping the scheduling logic in crontab.

That's the rough approach outlined in tutorials like below:


share|improve this answer
Okey, that was also a conclusion I was going to.. Thanks for your answer. –  jverdeyen Feb 26 '13 at 20:12
add comment

If you're on Linux, you can use the process control extension to daemonize a PHP process that does what you're looking to do:


I've used cron-expression in the past for doing something similar:


You wouldn't want to run the query every minute, though, unless there aren't many schedules. It would be easier to store them in memory and signal the daemon every time something is changed (kind of like cron).

Honestly, you probably don't need MySQL for this at all. Something as simple as a flat-file database would probably do what you want to do.

If you're trying to do this because you don't have access to cron, you may want to look at Drupal and WordPress for some insight - they both implement a "cron without cron" scheduling system, and it may be decoupled enough to pull directly from those projects.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.