为什么 crontab 不执行我的 PHP 脚本?

2023-08-19php开发问题
4

本文介绍了为什么 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 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    The End

  • 相关推荐

    PHP实现DeepL翻译API调用
    DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
    2025-08-20 php开发问题
    168

    PHP通过phpspreadsheet导入Excel日期数据处理方法
    PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
    2024-10-23 php开发问题
    287

    mediatemple - 无法使用 codeigniter 发送电子邮件
    mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
    2024-08-23 php开发问题
    11

    Laravel Gmail 配置错误
    Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
    2024-08-23 php开发问题
    16

    将 PHPMailer 用于 SMTP 的问题
    Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
    2024-08-23 php开发问题
    4

    关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
    Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
    2024-08-23 php开发问题
    17