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


how to run a php script daily with the cron job on Ubuntu os

命令运行我正在使用 ubuntu 12 和灯服务器。我想每 1 小时运行一个 php 脚本。我创建了一个 crontab 来执行它,如果我使用命令 crontab -l 检查我的 cron 列表,它会像这样显示

# 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 a.m 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)
# 
# m h  dom mon dow   command

这是我的PHP脚本

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

但它没有执行

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

您可以使用 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

要找出您的 cron 出了什么问题,您可以在终端中键入以下命令:

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

系统日志包含所有 crons 日志。

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

您还可以将所有错误重定向到文件:

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

make script file/etc/scripts/cron.sh with include

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

然后保存它

chmod +x cron.sh

然后继续/etc/crontab

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

省省吧并等待

这是 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

这样,您的脚本将每小时开始执行一次。