Cron工作在Ubuntu的php [英] Cron job on Ubuntu for php

查看:68
本文介绍了Cron工作在Ubuntu的php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器上使用Ubuntu,我正在使用Putty来访问。我想为我的php网站创建cronjobs。我如何做到这一点?

解决方案

如果你的意思是你希望你的php网站做一些常规的任务,

1)你使用cron定期拉一个页面。您可以使用基于文本的浏览器,例如ly。你拉这样的脚本:



* * * * * / usr / bin / lynx http://yourhost.com/cron.php -dump> / dev / null



(这将每分钟调用一次,这样你可以在应用程序中构建自己的日程表)



2)使用命令行php解释器调用脚本:



* * * * / usr / bin / php /path/to/cron.php> / dev / null



一般来说,解决方案二更好。但是你需要访问框。如果您不能在主机上安装cron,则可以从不同的主机触发cron。



还要注意使用命令行版本的php 。在debian(和潜在的其他系统)上,可能有cgi,cli和mod_php的单独的php.ini文件。如果您已经定制了您的配置,请确保命令行php使用正确的配置。您可以使用以下方法进行测试:



/ usr / bin / php -i |



为了回应dimo的评论,我做了一些基准测试。我用lynx,wget和php-cli调用了一个简单的本地php脚本(只是echos测试):

  kbsilver:temp kbeyer $ time。 wget.sh 

real 0m14.223s
user 0m2.906s
sys 0m6.335s

(命令:wget -O / dev / null localhost / test.php; 2> / dev / null)

kbsilver:temp kbeyer $ time。 lynx.sh

real 0m26.511s
用户0m5.789s
sys 0m9.467s

(命令:lynx -dumplocalhost / test。 php;> / dev / null)




kbsilver:temp kbeyer $ time。 php_cli.sh

real 0m54.617s
用户0m28.704s
sys 0m18.403s

(命令:/ opt / local / bin / php /www/htdocs/test.php> / dev / null)

服务器 lighttpd php(fastcgi)使用apc(在Mac OS X上)。





因此, php-cli 不是那么惊讶,因为其他方法重用已经运行的php线程与操作码缓存。



因此,使用php-cli的唯一真正的优势是安全性,因为脚本不能从外部提供,因为你可以把它放在docroot之外。



(这个测试显然不是100%准确,但在我看来,差异是很明显的)


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?

解决方案

If you mean that you want your php site to do some regular tasks, there are two possible ways.

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) You call your script with the command line php interpreter:

* * * * * /usr/bin/php /path/to/cron.php > /dev/null

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.

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 | less

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) 

Server is lighttpd, php(fastcgi) with apc (on Mac OS X).

It turns out that indeed wget is the best tool for the job regarding speed.

So the result of php-cli is not that suprising as the other methods reuse an already running php thread with opcode cache.

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.

(This test is obviously not 100% accurate, but the differences are quite obvious in my opinion)

这篇关于Cron工作在Ubuntu的php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆