Heim  >  Artikel  >  Backend-Entwicklung  >  如何使用Linux的Crontab定时执行PHP脚本的方法_php技巧

如何使用Linux的Crontab定时执行PHP脚本的方法_php技巧

WBOY
WBOYOriginal
2016-05-17 09:13:541434Durchsuche

下面介绍Crontab的两种方法。

一、在Crontab中使用PHP执行脚本

就像在Crontab中调用普通的shell脚本一样(具体Crontab用法),使用PHP程序来调用PHP脚本。
每一小时执行myscript.php如下:

复制代码 代码如下:

# crontab -e
00 * * * * /usr/local/bin/php /home/john/myscript.php

/usr/local/bin/php为PHP程序的路径。

二、在Crontab中使用URL执行脚本

如果你的PHP脚本可以通过URL触发,你可以使用lynx或curl或wget来配置你的Crontab。
下面的例子是使用Lynx文本浏览器访问URL来每小时执行PHP脚本。Lynx文本浏览器默认使用对话方式打开URL。但是,像下面的,我们在lynx命令行中使用-dump选项来把URL的输出转换来标准输出。
复制代码 代码如下:

00 * * * * lynx -dump http://www.jb51.net/myscript.php

下面的例子是使用CURL访问URL来每5分执行PHP脚本。Curl默认在标准输出显示输出。使用”curl -o”选项,你也可以把脚本的输出转储到临时文件。
复制代码 代码如下:

*/5 * * * * /usr/bin/curl -o temp.txt http://www.jb51.net/myscript.php

下面的例子是使用WGET访问URL来每10分执行PHP脚本。-q选项表示安静模式。”-O temp.txt”表示输出会发送到临时文件。
复制代码 代码如下:

*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.jb51.net/myscript.php
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn