在 Ubuntu 上为 php 执行 Cron 作业

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1210930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:33:41  来源:igfitidea点击:

Cron job on Ubuntu for php

phpubuntucron

提问by webkul

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?

我在服务器上使用 Ubuntu,我使用 Putty 访问。我想为我的 php 站点创建 cronjobs。我怎样才能做到这一点?

回答by kbeyer

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. You can do this with a text-based browser, e.g. lynx. You pull your script like this:

1)您使用cron定期拉取某个页面。您可以使用基于文本的浏览器(例如 lynx)来执行此操作。你像这样拉你的脚本:

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

* * * * * /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 解释器调用您的脚本:

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

* * * * * /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.

通常解决方案二更好。但是,您需要访问该框。如果您无法在主机上安装 cron,则解决方案一中的 cron 可以从不同的主机触发。

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:

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

/usr/bin/php -i | less

/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:

为了回应 dimo 的评论,我做了一些基准测试。我用 lynx、wget 和 php-cli 调用了一个简单的本地 php 脚本(它只是回应“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-cliis 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%准确,但在我看来差异还是很明显的)

回答by Jon

I'm asssuming you want to backup your PHP site? Edit the crontab using:

我假设您想备份您的 PHP 站点?使用以下命令编辑 crontab:

crontab -e

This will start up an instance of vi in which you can edit the crontab, press ifor insert mode. You then need to put in the information for when the cron entry will run and the command to run at that time, e.g.:

这将启动一个 vi 实例,您可以在其中编辑 crontab,按i进入插入模式。然后,您需要输入有关 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. Exit and quit vi with :wq

因此,上面的命令将在每天晚上 10:30 将您的 phpsite tar gzip 压缩到 /var/www/phpsite 中。使用:wq退出并退出 vi

See this for further reference:

请参阅此以进一步参考:

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

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