问题描述
我需要每1小时运行一个PHP文件.
I need to run a PHP file every 1 hour.
我正在做的是:
sudo crontab -e
sudo crontab -e
(在编辑器中)* 01 * * */usr/bin/php/var/www/devicecheck.php
(In the editor) * 01 * * * /usr/bin/php /var/www/devicecheck.php
但是以某种方式,它不起作用.该命令在命令行上工作.在此之前,我尝试使用php/var/www/devicecheck.php
But somehow, it's not working. The command works on the command line. Before this, I was trying php /var/www/devicecheck.php
有什么建议吗?
推荐答案
要每1小时执行一次devicecheck.php,请尝试以下操作:
To execute devicecheck.php every 1 hour try the following:
方法A ::使用crontab中的php执行脚本
# crontab -e
00 * * * * /usr/bin/php/var/www/devicecheck.php
方法B:使用crontab中的URL运行php脚本
如果可以使用URL调用您的php脚本,则可以使用Lynnx,curl或wget来设置crontab,如下所示.
If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab as shown below.
以下脚本通过使用lynx文本浏览器调用URL,从而执行php脚本(每小时执行一次).Lynx文本浏览器默认情况下以交互方式打开URL.但是,如下所示,lynx命令中的-dump选项会将URL的输出转储到标准输出.
The following script executes the php script (every hour) by calling the URL using the lynx text browser. Lynx text browser by default opens a URL in the interactive mode. However, as shown below, the -dump option in lynx command, dumps the output of the URL to the standard output.
00 * * * * lynx -dump http://www.yourwebsite.com/yourscript.php
以下脚本通过使用CURL调用URL来执行php脚本(每5分钟执行一次).默认情况下,Curl在标准输出中显示输出.使用"curl -o"选项,您还可以将脚本的输出转储到一个临时文件,如下所示.
The following script executes the php script (every 5 minutes) by calling the URL using CURL. Curl by default displays the output in the standard output. Using the "curl -o" option, you can also dump the output of your script to a temporary file as shown below.
*/5 * * * * /usr/bin/curl -o temp.txt http://www.yourwebsite.com/yourscript.php
以下脚本通过使用WGET调用URL来执行php脚本(每10分钟执行一次).-q选项表示完全模式."-O temp.txt"表示输出将发送到临时文件.
The following script executes the php script (every 10 minutes) by calling the URL using WGET. The -q option indicates quite mode. The "-O temp.txt" indicates that the output will be send to the temporary file.
*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.yourwebsite.com/yourscript.php
更新::
# chmod a+x /home/username/yourscript.php
# crontab -e
00 * * * * /home/username/yourscript.php
这篇关于使用crontab运行PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!