为什么crontab不执行我的PHP脚本? [英] Why is crontab not executing my PHP script?
问题描述
我已经建立了一个php文件来检查某些结果,因此我需要设置cronjob.
I have built one php file to check some result, so that I need to setup a cronjob.
我将其设置为每30分钟运行一次,以便将结果发送出去.但是,我不知道为什么我的crontab不能每隔30分钟运行一次.
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.
这是我设置crontab的方法:
Here is how I set the crontab:
*/30 * * * * php /var/www/html/result.php
我已确认我的文件目录正确.我不确定时间部分:是否可以使用*/30 * * * *
或30 * * * *
?我设置了*/30 * * * *
,但没有用.
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 * * * * php /var/www/html/result.php
为什么它不起作用有多种可能性:
There are multiple possibilities why it is not working:
-
首先,重要的是检查
php /var/www/html/result.php
的简单执行情况.这是必需的.但不幸的是,完成此操作并不意味着问题已解决.
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.
必须添加php
二进制文件的路径.
The path of the php
binary has to be added.
*/30 * * * * php /var/www/html/result.php
要更改为
*/30 * * * * /usr/bin/php /var/www/html/result.php
或来自which php
的任何内容.
检查脚本对运行crontab的用户的权限.
Check the permission of the script to the user running the crontab.
授予文件执行权限:chmod +x file
.并确保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.
为了更加安全,您还可以在脚本顶部添加php路径,例如:
To be safer, you can also add the php path in the top of the script, such as:
#!/usr/bin/php -q
<?php
...
?>
确保用户有权使用crontab.检查他是否在/etc/cron.d/deny
文件中.另外,进行基本测试以查看它是否是crontanb或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
使用-f
选项执行脚本:
Use the -f
option to execute the script:
*/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.
这篇关于为什么crontab不执行我的PHP脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!