我在服务器上使用 Ubuntu,我使用 Putty 访问.我想为我的 php 站点创建 cronjobs.我怎样才能做到这一点?
I'm using Ubuntu on the server and I'm using Putty to access. I want to create cronjobs for my php site. How can I do this?
如果您的意思是希望您的 php 站点执行一些常规任务,则有两种可能的方法.
If you mean that you want your php site to do some regular tasks, there are two possible ways.
1) 您使用 cron 定期拉取某个页面.您可以使用基于文本的浏览器执行此操作,例如猞猁.你像这样拉你的脚本:
1) You use cron to pull a certain page regularly. You can do this with a text-based browser, e.g. lynx. You pull your script like this:
* * * * */usr/bin/lynx http://yourhost.com/cron.php -dump >/dev/null
(这将每分钟调用一次.这样您就可以在应用程序中构建自己的时间表)
(This will call it every minute. That way you can build your own schedule inside your application)
2) 你用命令行 php 解释器调用你的脚本:
2) You call your script with the command line php interpreter:
<代码>* * * * */usr/bin/php/path/to/cron.php >/dev/null
通常解决方案二更好.但是,您需要访问该框.如果您无法在主机上安装 cron,则解决方案一中的 cron 可以从不同的主机触发.
Generally solution two is better. However you will need access to the box. The cron in solution one can be triggered from a different host, if you cannot install crons on the host.
还要注意使用命令行版本的 php 的常见陷阱.在 debian(以及可能的其他系统)上,可能有单独的 php.ini 文件用于 cgi、cli 和 mod_php.如果您自定义了配置,请确保命令行 php 使用的是正确的配置.您可以使用以下方法进行测试:
Also beware of a common pitfall using the command line version of php. On debian (and potentially other systems) there may be seperate php.ini files for cgi, cli and mod_php. If you have customized your configuration make sure that the command line php is using the correct one. You can test this with:
<代码>/usr/bin/php -i |少
为了回应 dimo 的评论,我做了一些基准测试.我用 lynx、wget 和 php-cli 调用了一个简单的本地 php 脚本(它只是回应test")1000 次:
In response to the comment by dimo i made some benchmarks. I called a simple local php script (which just echos "test") 1000 times with lynx, wget and php-cli:
kbsilver:temp kbeyer$ time . wget.sh
real 0m14.223s
user 0m2.906s
sys 0m6.335s
(Command: wget -O /dev/null "localhost/test.php"; 2> /dev/null)
kbsilver:temp kbeyer$ time . lynx.sh
real 0m26.511s
user 0m5.789s
sys 0m9.467s
(Command: lynx -dump "localhost/test.php"; > /dev/null)
kbsilver:temp kbeyer$ time . php_cli.sh
real 0m54.617s
user 0m28.704s
sys 0m18.403s
(Command: /opt/local/bin/php /www/htdocs/test.php > /dev/null)
服务器是 lighttpd
,php(fastcgi)
带有 apc(在 Mac OS X 上).
Server is lighttpd
, php(fastcgi)
with apc (on Mac OS X).
事实证明,就速度而言,wget 确实是这项工作的最佳工具.
It turns out that indeed wget is the best tool for the job regarding speed.
所以 php-cli
的结果并不令人惊讶,因为其他方法重用了一个已经运行的带有操作码缓存的 php 线程.
So the result of php-cli
is not that suprising as the other methods reuse an already running php thread with opcode cache.
因此,使用 php-cli 的唯一真正优势是安全性,因为脚本将无法从外部访问,因为您可以将其放在 docroot 之外.
So the only real advantage of using php-cli is security as the script will not be available from outside as you can put it outside the docroot.
(这个测试显然不是100%准确,但在我看来差异还是很明显的)
(This test is obviously not 100% accurate, but the differences are quite obvious in my opinion)
这篇关于在 Ubuntu 上为 php 执行 Cron 作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!