开发者

Is there a way in which a PHP or shell script can setup a cron job on a web server?

I am writing a component for Joomla and there is a specific task that requires an update to some stats every so often. I would like to setup a cron job to do this. The only problem is that requires the user to go and setup the cron to run the php update stats script.

On installation of the component how can I automatically setup a cron job for the user? Is this possible?

I've seen this implemented in the Akeeba backup pro component for J开发者_开发百科oomla, so I was hoping that I would be able to do the same thing.

Thanks


In theory you can create a crontab file and call it from PHP

<?php
exec("crontab $path_to_cron_file");

in practice it depends on wether the server (if you're on a shared hosting) allows you to do that.


All you need to do is write a line to the crontab file, generally stored in /var/spool/cron/crontabs/username. The cron daemon will see that the file modification time has changed and reload it automatically when it wakes up to do its checks


Another option (less desirable from a server load perspective but easier for users) would be to create a plugin that is run each time a visitor visits the site. It could check whether the process has been run in a specified time and then run it if it needs to be run.

iJoobi.com has another solution where they have set up a server to run cron tasks, which would then ping the specific URL on the website to kick off the process. (http://www.ijoobi.com/Help/jNews/jNews-Cron-Task.html)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜