安排Crontab运行PHP脚本

时间:2016-04-01 12:02:53

标签: php crontab

我试图通过cron标签在路径/var/www/html/rss_feed/mirror.php上运行PHP文件。为此,我执行了以下步骤。

sudo crontab -e

然后我通过插入以下代码来编辑文件

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# avt 5 a.m every week with:
# 0 5 * * 1 tar -zcf /ar/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
*/2 * * * * /usr/bin/php /var/www/html/rss_feed/nbt_times.php
#
# m h  dom mon dow   command

如下所示,为了运行php文件,每隔2分钟插入一行代码来安排cron选项卡。

*/2 * * * * /usr/bin/php /var/www/html/rss_feed/nbt_times.php

我甚至试过了

2 * * * * lynx -dump http://192.168.0.232/rss_feed/mirror.php > /dev/null 2>&1

甚至这个

2  * * * * /your/path/to/php /var/www/html/rss_feed/nbt_times.php

以及来自不同文章的更多格式。

php脚本未运行。我需要将代码放在其他地方吗?或者我走错了路。能否请你帮我找出问题所在。

2 个答案:

答案 0 :(得分:0)

它应该可以工作,但需要在这里得到一些澄清,文件名似乎是不同的一次它的mirror.php和另一次它的nbt_times.php

哪个php文件必须在必须指定的特定时间运行。根据您的指定,您将运行/var/www/html/rss_feed/mirror.php,然后在第一个命令上指定mirrror.php。

第二和第三个命令将在每小时的第2分钟运行作业。喜欢(4:02,5:02,6:02等)

试试这个crontab

*/2 * * * * /usr/bin/php /var/www/html/rss_feed/mirror.php

这将每2分钟运行一次,希望这有效。

答案 1 :(得分:0)

尝试使用php5-cli:

*/2 * * * * /usr/bin/php5-cli -f /var/www/html/rss_feed/nbt_times.php

还要在日志中添加写入并在分钟后查看:

* * * * * /usr/bin/php /var/www/html/rss_feed/nbt_times.php >>log.log 2>>log.err

检查php文件的权限。制作0777进行检查。尝试用一行创建最简单的php文件,然后通过cron运行它。