php - 在centos上的cron作业中执行PHP

标签 php cron centos

我的 cronjob 实验需要帮助,我无法完成我想要的。以下是我所做的事情。

  • /var/www/html/bob/test/index.php 中创建一个 php 文件
  • 将目录和文件的权限设置为可执行(0777)
  • 指定一个 cronjob

  • 我的 index.php
    <?php 
    $myfile = fopen("testfile.txt", "w");  
    ?>
    

    我对 的各种测试参数crontab -e 这没有用
    */1 * * * * root /usr/bin/php /var/www/html/bob/test/index.php
    */1 * * * * root /var/www/html/bob/test/index.php
    */1 * * * * root php -q /var/www/html/bob/test/index.php
    */1 * * * * root /var/www/html/bob/test/index.php
    

    看着我的一些 /var/log/cron 我没有看到任何错误指示。
    Sep 6 15:55:01 localhost CROND[4866]: (root) CMD (root /var/www/html/bob/test/index.php)
    Sep 6 15:56:01 localhost CROND[4872]: (root) CMD (root /var/www/html/bob/test/index.php)
    Sep 6 15:57:01 localhost CROND[4878]: (root) CMD (root /var/www/html/bob/test/index.php)
    

    最后,当我通过 #php /var/www/html/bob/test/index.php 运行我的 php代码执行良好并创建该文本文件。

    最佳答案

    您需要删除 root来自你的 cronjob。日志显示它正在尝试使用 /var/www/html/bob/test/index.php 执行名为 root 的程序。作为参数。

    相反,请使用以下内容作为您的 cronjob:

    */1 * * * * /usr/bin/php /var/www/html/bob/test/index.php
    

    This answer声明如果要设置运行 cronjob 的用户,则必须编辑 /etc/crontab直接,而不是使用 crontab -e .

    关于php - 在centos上的cron作业中执行PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46070019/

    相关文章:

    PHP 引发 SQL 语法错误

    php - 在 PHP 中连接到 DB2(从 MySQL 更改)

    PHP set_time_limit 限制

    terminal - 对于在 centos 7 中找不到的 root 访问 Composer

    php - 在运行的 Plesk 11.5 服务器上更新 PHP/MySQL 的正确方法?

    centos - plesk - apache 文件所有者

    php - 使用 preg_match 检查文本格式

    php - 更新数据库而不重新加载页面

    linux - 从 root 帐户执行 cron 作业是否安全?

    linux - 每 15 分钟运行一次 crontab 不适用于 linux redhat