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

2 php linux cron crontab ubuntu-12.04

命令运行我正在使用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
Run Code Online (Sandbox Code Playgroud)

这是我的PHP脚本

0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php
Run Code Online (Sandbox Code Playgroud)

但它没有执行

我怎么能检查它为什么不起作用,请帮忙

Khe*_*dun 6

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

grep -i "cron1.php" /var/log/syslog
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)


Har*_*tel 5

您可以使用crontab添加/删除/编辑cronjobs.

按Alt + Ctrl + T打开终端.

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

chmod +x YOURSCRIPT
Run Code Online (Sandbox Code Playgroud)

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

crontab -e
Run Code Online (Sandbox Code Playgroud)

像这样添加你的cronjob:

0 * * * * /usr/local/bin/php path/of/php/file
Run Code Online (Sandbox Code Playgroud)

而已!

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

crontab -l
Run Code Online (Sandbox Code Playgroud)

有关crontab运行的更多信息:

crontab --help
Run Code Online (Sandbox Code Playgroud)

要么

man crontab
Run Code Online (Sandbox Code Playgroud)