tags:

views:

273

answers:

4

Hello,

i am wanting to add a section in my php application to add / remove scheduled task.

Something similar to what they use in vBulletin Forum.

Does anyone have any ideas what they use? are they using Cron Jobs or do they have something custom done.

Can anyone guide me on how i can accomplish something similar to that, basically i just want my script to add or remove cron jobs / scheduled task.

But i don't want to use for example the cPanel Cron interface, i would like to accomplish this from within my script.

Any help is greatly appreciated.

+1  A: 

http://ryanfaerman.com/php-crontab-manager/

is a php class that lets you access a crontab from PHP using a class.

CodeJoust
As the linked post says, cron's behavior might vary from machine to machine. If your php script runs as user 'apache', then you have to be sure that user is allowed to use crontab, which involves locating the cron.deny and cron.allow files. 'man crontab' (if you have shell access) can tell you where to find those files and how they're interpreted... If you're going to package your code for deployment in unknown environments, though, you may want to 'fake it' as in evolve's answer.
grossvogel
+1  A: 

I believe vBulletin "fakes" cron jobs. Any user which accesses the page executes code which checks to see if any tasks need to be run.

If however, no users visit the website, none of the cron jobs will execute, the method is 100% reliant on page views.

Also, to prevent delayed page rendering on users who execute the cron jobs they are executed after the page has been rendered.

Again, this is just what I think is happening, I haven't looked at the code myself.

Hope this helps.

evolve
A: 

My host has exec() disabled :( and i just found out that crons can't be used with out it from php :(

Is there a class of those fake types mentioned above? the one that fakes it.

i don't require any advanced functionality, just need to add a task / view them and delete them.

Zubair1
if you have pcntl installed, you can use this:http://www.phpclasses.org/package/4140-PHP-Database-driven-PHP-job-scheduler-like-cron.html (can't vouch for quality, here)
grossvogel
Just read on php.net that pctl is not recommended for web server environments, so you should probably disregard that last comment.
grossvogel
+2  A: 

If you don't have the exec() function enabled, you have to rely on manually setting at least one cron job via some interface and then managing individual jobs in your script.

What I do for example is this

  1. I have a script heartbeat.php which is executed every minute
  2. This script checks a MySQL table crontab and checks if there is any row (job) with time_to_process <= NOW()
  3. If there is, it loads the class defined for the specific row from crontab table
  4. The class implements an interface with run() method and I only call $cronJob->run() to make it run
  5. The class then handles it's rescheduling via crontab table once it finishes

A good example is to reset daily views on some items. The job is scheduled at May 1, 2010 00:00. Once the job ran, it schedules itself for the next day automatically.
You can then implement automatic reset of failed jobs and many other features from within PHP.

michal kralik
Also known as 'poor man's cron'.
CodeJoust
Thanks alot :) This is almost what i needed although i didn't want to touch the cPanel's interface for cron, but i figured the best solution would be to do something similar to what you have stated; but instead of manually creating a cron first i would rather just call that cron-manger script from within the website pages when ever a user executes a request to the page.I think i am sure now that vBulletin doesn't use cron's either - because for that they would have to rely on exec() too which is disabled on my host, yet the vbulletin still works with full functionality of those schedulers.
Zubair1