Can we configure cron job's time interval through PHP script?
Can we co开发者_C百科nfigure cron job's time interval through PHP script, so that the time interval should not be set manually, but through a PHP script, whether it takes time interval from Database or fixed (but from within the PHP code).
Thanks in advanceI think it is much better to let your application control the frequency of events instead of the cronjob. Let the cronjob run a certain action of your application every minute. The action then for example checks a database table named cronjobs and runs the jobs marked for running by either a frequency number or a timestamp.
If you do it like this, you can add new jobs programmatically from everywhere, e.g. via an cronjob interface. The solution is easier to maintain, to test and to document.
There are two ways. Either, re-create the crontab on every change of the desired value or have the cron job fire regularly, e.g. every minute, and test the starting condition from within the script.
If there are sufficient server privileges, you should be able to do so using a System level call from your PHP script, using system().
The best to do this, would probably be by writing a PHP script that is capable of creating a crontab
-file (and running your script with sufficient privileges to do this).
This site offers a pretty good tutorial.
However, dependency on cron
will make your code dependant on platforms that support cron
. Thus another solution would be to implement the timed-execution-logic in PHP itself (i.e. loop the code, and check the time yourself).
You could combine the two solutions above, by creating a Scheduler
-interface that you can give a script, and it will make sure it's executed at the appropriate times. This way you can implement it using cron
on linux, but in another way on Windows:
interface Scheduler {
function schedule($script);
}
class CronScheduler {
function schedule($script) {
append_cronjob($script);
}
}
Though, I might be overdoing it here.
If you want to do this I would suggest you to use Message Queue Beanstalkd instead. It can do delayed put programmatic and is very fast. I advice you to use pheanstalk to talk the Beanstalkd.
精彩评论