简体   繁体   English

Cron在Ubuntu上为php工作

[英]Cron job on Ubuntu for php

I'm using Ubuntu on the server and I'm using Putty to access. 我在服务器上使用Ubuntu而我正在使用Putty访问。 I want to create cronjobs for my php site. 我想为我的php网站创建cronjobs。 How can I do this? 我怎样才能做到这一点?

If you mean that you want your php site to do some regular tasks, there are two possible ways. 如果你的意思是你希望你的php网站做一些常规任务,有两种可能的方法。

1) You use cron to pull a certain page regularly. 1)您使用cron定期拉出某个页面。 You can do this with a text-based browser, eg lynx. 您可以使用基于文本的浏览器执行此操作,例如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: 2)使用命令行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. 如果您无法在主机上安装crons,则可以从其他主机触发解决方案一中的cron。

Also beware of a common pitfall using the command line version of php. 还要注意使用php的命令行版本的常见陷阱。 On debian (and potentially other systems) there may be seperate php.ini files for cgi, cli and mod_php. 在debian(以及可能的其他系统)上,可能存在用于cgi,cli和mod_php的单独的php.ini文件。 If you have customized your configuration make sure that the command line php is using the correct one. 如果您已自定义配置,请确保命令行php使用正确的配置。 You can test this with: 您可以使用以下方法测试:

/usr/bin/php -i | less

In response to the comment by dimo i made some benchmarks. 为了回应dimo的评论,我做了一些基准测试。 I called a simple local php script (which just echos "test") 1000 times with lynx, wget and php-cli: 我用lynx,wget和php-cli调用了一个简单的本地php脚本(只是echos“test”)1000次:

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). 服务器是lighttpdphp(fastcgi)和apc(在Mac OS X上)。

It turns out that indeed wget is the best tool for the job regarding speed. 事实证明,wget确实是速度工作的最佳工具。

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的结果并不令人惊讶,因为其他方法重用已经运行的带有操作码缓存的php线程。

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. 所以使用php-cli的唯一真正优势是安全性,因为脚本不能从外部获得,因为你可以把它放在docroot之外。

(This test is obviously not 100% accurate, but the differences are quite obvious in my opinion) (这个测试显然不是100%准确,但在我看来差异非常明显)

I'm asssuming you want to backup your PHP site? 我很想你要备份你的PHP网站? Edit the crontab using: 使用以下命令编辑crontab:

crontab -e

This will start up an instance of vi in which you can edit the crontab, press i for insert mode. 这将启动一个vi实例,您可以在其中编辑crontab,按i进行插入模式。 You then need to put in the information for when the cron entry will run and the command to run at that time, eg: 然后,您需要输入cron条目运行时的信息以及当时运行的命令,例如:

30 10 * * * tar -zcvf ./myphpsite.tar.gz /var/www/phpsite

So the command above will tar gzip your phpsite in /var/www/phpsite at 10:30pm every day. 因此,上面的命令将每天晚上10:30在/ var / www / phpsite中对您的phpsite进行gzip。 Exit and quit vi with :wq 退出并退出vi :wq

See this for further reference: 有关进一步参考,请参阅此

http://www.adminschoice.com/docs/crontab.htm http://www.adminschoice.com/docs/crontab.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM