为什么 crontab 不执行我的 PHP 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7397469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is crontab not executing my PHP script?
提问by user941885
I have built one php file to check some result, so that I need to setup a cronjob.
我已经构建了一个 php 文件来检查一些结果,所以我需要设置一个 cronjob。
I set one to run every 30 minute, so that the results will be send. However, I don't know why my crontab did not run after every 30 minute.
我将一个设置为每 30 分钟运行一次,以便发送结果。但是,我不知道为什么我的 crontab 没有在每 30 分钟后运行一次。
Here is how I set the crontab:
这是我设置 crontab 的方法:
*/30 * * * * php /var/www/html/result.php
I have confirmed my file directory is correct. What I not sure is about the timing part: isn't it possible to use */30 * * * *
or 30 * * * *
? I set */30 * * * *
and did not work.
我已经确认我的文件目录是正确的。我不确定关于计时部分:是否可以使用*/30 * * * *
或30 * * * *
?我设置*/30 * * * *
并没有工作。
回答by fedorqui 'SO stop harming'
Given
给定的
*/30 * * * * php /var/www/html/result.php
There are multiple possibilities why it is not working:
它不工作的原因有多种可能:
First of all it is important to check if the simple execution of
php /var/www/html/result.php
. This is required. But unfortunately, accomplishing this does not mean that the problem is solved.The path of the
php
binary has to be added.*/30 * * * * php /var/www/html/result.php
to be changed to
*/30 * * * * /usr/bin/php /var/www/html/result.php
or whatever coming from
which php
.Check the permission of the script to the user running the crontab.
Give execution permission to the file:
chmod +x file
. And make sure the crontab is launched by a user having rights to execute the script. Also check if the user can access the directory in which the file is located.To be safer, you can also add the php path in the top of the script, such as:
#!/usr/bin/php -q <?php ... ?>
Make sure the user has rights to use crontab. Check if he is in the
/etc/cron.d/deny
file. Also, make a basic test to see if it is a crontanb or php problem.* * * * * touch /tmp/hello
Output the result of the script to a log file, as William Niu suggested.
*/30 * * * * /usr/bin/php /var/www/html/result.php > /tmp/result
Use the
-f
option to execute the script:*/30 * * * * /usr/bin/php -f /var/www/html/result.php > /tmp/result
Make sure the format in crontab is correct. You can do so for example using the site Crontab.guru.
首先,重要的是检查
php /var/www/html/result.php
. 这是必需的。但不幸的是,完成这一点并不意味着问题就解决了。php
必须添加二进制文件的路径。*/30 * * * * php /var/www/html/result.php
改为
*/30 * * * * /usr/bin/php /var/www/html/result.php
或任何来自
which php
.检查脚本对运行 crontab 的用户的权限。
赋予文件执行权限:
chmod +x file
. 并确保 crontab 是由有权执行脚本的用户启动的。还要检查用户是否可以访问文件所在的目录。为了更安全,还可以在脚本顶部添加php路径,如:
#!/usr/bin/php -q <?php ... ?>
确保用户有权使用 crontab。检查他是否在
/etc/cron.d/deny
文件中。另外,做一个基本的测试,看看是crontanb还是php的问题。* * * * * touch /tmp/hello
按照William Niu 的建议,将脚本的结果输出到日志文件中。
*/30 * * * * /usr/bin/php /var/www/html/result.php > /tmp/result
使用
-f
选项来执行脚本:*/30 * * * * /usr/bin/php -f /var/www/html/result.php > /tmp/result
确保 crontab 中的格式正确。例如,您可以使用站点Crontab.guru来做到这一点。
To sum up, there are many possible reasons. One of them should solve the problem.
总结一下,可能的原因有很多。其中之一应该可以解决问题。
回答by William Niu
It may be because php is not in the path. crontab
has a very minimal path
. So, include the full path for your php program.
可能是因为php不在路径中。crontab
有一个非常小的path
. 所以,包括你的 php 程序的完整路径。
you can test your cron commands by piping the output to a file, e.g.
您可以通过将输出管道传输到文件来测试您的 cron 命令,例如
*/30 * * * * php /var/www/html/result.php > /tmp/result.log
From this reference page, under "Crontab Environment":
从此参考页,在“Crontab 环境”下:
cron invokes the command from the user's HOME directory with the shell, (/usr/bin/sh). cron supplies a default environment for every shell, defining:
cron 使用 shell (/usr/bin/sh) 从用户的 HOME 目录调用命令。cron 为每个 shell 提供一个默认环境,定义:
HOME=user's-home-directory
LOGNAME=user's-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh
Also, /30
syntax might not be supported by all platforms, so, try to change it to 0,30
instead.
此外,/30
并非所有平台都支持语法,因此,请尝试将其更改为0,30
。
回答by Willem Abrie
Had a similar issue; from command line, it worked, but from cron, no go.
有一个类似的问题;从命令行,它工作,但从 cron,不行。
had a "include ("./connect.php"); in my php code for the db stuff.
有一个“include ("./connect.php"); 在我的 php 代码中用于 db 的东西。
Removed that, and added the connect.php code directly into the php script, and it worked from cron.
删除它,并将 connect.php 代码直接添加到 php 脚本中,它在 cron 中工作。
回答by nifo
I had a similar issue on Ubuntu 14.04.1
and the problem turned out to be the way I was modifying the crontab:
我有一个类似的问题Ubuntu 14.04.1
,结果问题出在我修改 crontab 的方式上:
I was using sudo crontab -e
instead of just crontab -e
and this caused my changes to be ignored.
我正在使用sudo crontab -e
而不仅仅是crontab -e
,这导致我的更改被忽略。
回答by KingColours
I had a funny one regarding this. Although my scripts would run manually, they wouldn't run from crontab.
关于这一点,我有一个有趣的问题。尽管我的脚本会手动运行,但它们不会从 crontab 运行。
Turns out that because the script was being run from /usr/bin/php
rather that the location of the file (as it does when I run it manually) my php require
wasn't finding the files I wanted. Changing that to reflect the full address fixed it.
事实证明,因为脚本是从/usr/bin/php
文件的位置而不是文件的位置运行的(就像我手动运行它时那样),我的 phprequire
没有找到我想要的文件。更改它以反映完整地址修复了它。
troubleshooting by running the script as /usr/bin/php -f /var/www/myfile.php
helped me find the issue
通过运行脚本进行故障排除/usr/bin/php -f /var/www/myfile.php
帮助我找到了问题
回答by Brian Jones
After a day of puzzling why my script would work directly (to send data in an email to a gmail account) I discovered that all the deliberate sends worked when I clicked the url and all the cron sends went into spam. No idea why but I thought I'd share it.
经过一天的困惑,为什么我的脚本可以直接工作(通过电子邮件将数据发送到 gmail 帐户),我发现当我单击 url 时,所有故意发送的内容都有效,并且所有 cron 发送的内容都变成了垃圾邮件。不知道为什么,但我想我会分享它。
回答by Paulo
Willem's answer showed me the way. In my case, I have a "include("connection.php")" inside my code. I changed connection.php to /my/full/path/connection.php. I have some rename() calls with the relative path, and I changed to the absolute path. That worked for me. I hope it can help someone else.
威廉的回答为我指明了方向。就我而言,我的代码中有一个“include(“connection.php”)”。我将 connection.php 更改为 /my/full/path/connection.php。我有一些使用相对路径的 rename() 调用,我更改为绝对路径。那对我有用。我希望它可以帮助别人。
回答by Siva Prakash
Easy and logical way:
简单而合乎逻辑的方式:
Checking the cron logs at /var/log/cron will give you very useful info
检查 /var/log/cron 中的 cron 日志将为您提供非常有用的信息
less /var/log/cron
少 /var/log/cron
Eg.,
例如。,
My cron entry is * * * * * /usr/bin/php /cat.php
<== Run cat.php every minute
我的 cron 条目是* * * * * /usr/bin/php /cat.php
<== 每分钟运行一次 cat.php
The log file will contain an entry similar to the one below every time a cron entry is run
每次运行 cron 条目时,日志文件都会包含一个类似于下面的条目
Jan 24 08:06:01 OlaTower CROND[13641]: (root) CMD (/usr/bin/php /cat.php)
Jan 24 08:07:01 OlaTower CROND[13641]: (root) CMD (/usr/bin/php /cat.php)
Here, the php command will be executed every minute and there will be an entry in the log file every minute
这里每分钟执行一次php命令,每分钟在日志文件中会有一个条目
If the entry is not there then crond is not even picking that cronjob. If the log entry is there and still you are not getting the desired output then there is something wrong with the command/application logic
如果条目不存在,则 crond 甚至不会选择该 cronjob。如果日志条目在那里,但您仍然没有获得所需的输出,则命令/应用程序逻辑有问题
回答by Trygve
Are you sure it is not running? If you use exec, realize that you are running from cron and the full path for everything is required, so instead of cp
, you'll need to use /bin/cp
.
你确定它没有运行?如果您使用 exec,请意识到您正在从 cron 运行并且需要所有内容的完整路径,因此cp
您需要使用/bin/cp
.
回答by Optimaz ID
You can use */30 * * * * wget http://my.domain.com/path/to/php/result.php
您可以使用 */30 * * * * wget http://my.domain.com/path/to/php/result.php
But Crontab executes the task using the current user that ran crontab -e
. When you use wget it's handled by Apache using the www-data
user/group pair
但是 Crontab 使用运行的当前用户执行任务crontab -e
。当您使用 wget 时,它由 Apache 使用www-data
用户/组对处理
First, make sure the script works as expected.
$ php /var/www/html/result.php
Second, edit the crontab for the Apache user account
$ sudo crontab -u www-data -e
or
$ sudo crontab -u root -e
Now add the crontab and output to a log file.
*/30 * * * * php /var/www/html/result.php > /tmp/result.log
首先,确保脚本按预期工作。
$ php /var/www/html/result.php
二、编辑Apache用户账号的crontab
$ sudo crontab -u www-data -e
或者
$ sudo crontab -u root -e
现在将 crontab 和输出添加到日志文件中。
*/30 * * * * php /var/www/html/result.php > /tmp/result.log