为什么 crontab 不执行我的 PHP 脚本? [英] Why is crontab not executing my PHP script?

查看:27
本文介绍了为什么 crontab 不执行我的 PHP 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个 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.

推荐答案

Given

*/30 * * * * php /var/www/html/result.php

它不工作的原因有多种可能:

There are multiple possibilities why it is not working:

  1. 首先检查php/var/www/html/result.php 是否简单执行很重要.这是必需的.但不幸的是,完成这并不意味着问题就解决了.

  1. 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 的任何内容.

or whatever coming from 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
    

  • 将脚本的结果输出到日志文件,如牛伟建议.

    */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.

    这篇关于为什么 crontab 不执行我的 PHP 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆