Question

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?

Was it helpful?

Solution

Almost all of the methods that you suggested are ugly, even though most will work.

My ugly alternative is to set up cron on a local machine with internet connectivity that runs a curl command ever few minutes.

It's just as ugly as all of your solutions, but it is slightly more reliable.

OTHER TIPS

I use a windows tool called VisualCron to ping http-password-protected scripts on remote servers that then execute specific tasks. V-C has nice email status notifications built in as well.

Note that this is very similar to Zack's post about using local cron+curl or cron+wget

http://www.visualcron.com http://www.visualcron.com/img/screenshots/v5/mainwindow/mainwindow_bluetheme.png

Have you ever used windows task scheduler or the at command? It is possible to use it as an alternate to CRON on UNIX. You can (1) write a PHP script that performs all necessary chores (2) set up a CRON job on UNIX or Scheduled Task on Windows to execute this script via PHP CLI.

It is also possible to execute the script by opening from HTTP from a remote machine. The remote machine can be a UNIX or Windows box. wget can be used instead of a web browser and it is available for both platforms.

I would opt for a second server / virtual machine to run these types of scheduled tasks. That second server can be offsite and perform other functions such as notify you when your primary server is unresponsive, etc.

By the way, what's your definition of "accurate?"

IMHO, you should write a layer above these methods...

User code <=> [User interface]<-> Your library <->[Technology interface] <=> Technology execution module (method)

Then you don't have to implement all methods, but if you or your client need a new method... all he has is to do is to implement your technology interface.

BTW, the "technology execution method" can be written as a PHP module for better performance / host-system integration.

If I were you, i would look for an existing library/module, and i wouldn't create a background process unless the underlying system cannot handle scheduled tasks (unix and windows do handle them).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top