I'm trying to come up with a list of different approaches to run scheduled tasks on PHP. My intention is to provide an universal way to run scheduled tasks in PHP. So far I have:
1) Analyze site traffic. If you receive 770 hits a day (which is one hit per 2 minutes), and you scheduled a task to run at 6:00 AM and a visitor requested a page at 5:59 AM, then run the task because the next visitor will arrive in 6:01 AM on average. Run = exec('/usr/bin/php -f /home/account/cron.php') in this case.
(+) Works on all platforms as long as the paths are correct.
(-) Requires some CPU power.
(-) Requires exec().
(-) Is not accurate on smaller sites or on sites with huge traffic spikes.
2) Improved version of the above. When the user requests the page and the task is meant to run, don't use exec() but include() after you have flushed the contents to the user.
(+) Works on all platforms.
(+) No exec()'s.
(-) Requires some CPU power.
(-) Is not accurate on smaller sites or on sites with huge traffic spikes.
3) Running a separate process background so that it is running in a constant loop. Provide an admin interface that let's you "start" and "end" the "service". It will then use fsockopen() to call a .php script that runs infinitely. It uses sleep() to not consume resources and to wake up when the time is right (see: time_sleep_until()). It could search for files and read them to understand when to run what tasks. One could create file "run-everyday-3.00am" that makes the scheduler to run the code inside of it.
(+) Works on all platforms.
(+) No exec()'s.
(+) Can be quite accurate (e.g. if it sleeps per a minute basis).
(-) Is not stable - a server crash stops the scheduler entirely.
(-) Some hosts don't like to have a process running 24/7/365 = resource hog?
4) Run exec('crontab') directly on Linux and alike.
(+) It is not a resource hog.
(+) Is accurate.
(-) exec().
(-) Does not work on all platforms.
5) Asking for cPanel credentials and making a POST to it to create/manage/remove crons.
(+) Is accurate
(+) It is not a resource hog.
(-) Bad for security
(-) Requires user details = decreased usability
(-) Does not work on all platforms (requires cPanel which does not work on Windows).
(-) Requires cPanel.
Any other ideas?