如何在Ubuntu操作系统上使用cron作业每日运行一个php脚本

命令来运行我正在使用Ubuntu 12和灯服务器。 我想每1小时运行一次php脚本。 我已经创build一个crontab来执行此操作,如果我检查我的cron列表与命令crontab -l它显示像这样

# Edit this file to introduce tasks to be run by cron. 0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php # 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 # at 5 am every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # mh dom mon dow command 

这是我的PHP脚本

 0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php 

但它没有执行

我如何检查为什么它不工作,请帮助

要找出你的cron有什么问题,你可以在你的终端中输入以下命令:

 grep -i "cron1.php" /var/log/syslog 

系统日志包含cron的所有日志。

尝试在终端运行代码/usr/bin/php5 -q /var/www/cronjobs/cron1.php来检查是否有错误。

您也可以将所有错误重定向到一个文件:

 0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php 2> /tmp/errorCron1.txt 

你可以使用crontab来添加/删除/编辑cronjobs。

按Alt + Ctrl + T打开终端。

首先通过运行确认脚本是可执行的:

 chmod +x YOURSCRIPT 

然后运行以下命令来添加你的cronjob:

 crontab -e 

像这样添加你的cronjob:

 0 * * * * /usr/local/bin/php path/of/php/file 

而已!

您可以运行以下命令来检查当前用户的crontab条目:

 crontab -l 

有关crontab运行的更多信息:

 crontab --help 

要么

 man crontab 

这是crontab参数的描述

 # Minute Hour Day of Month Month Day of Week Command # (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat) 0 2 12 * * /usr/bin/find 

运行你的脚本每小时使用下面的crontab条目。

 0 */1 * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php 

这样你的脚本就会开始执行每一个小时。

使用包含脚本文件/etc/scripts/cron.sh

 cd /var/www/ /usr/bin/php cron.php 

保存它

 chmod +x cron.sh 

然后继续/ etc / crontab

 15 15 * * * root /etc/scripts/cron.sh 

保存并等待