【问题标题】:CRON job not executing and returning errorsCRON 作业未执行并返回错误
【发布时间】:2012-10-31 10:14:13
【问题描述】:

我在 cron 中运行以下代码,

<?php
    $_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);
    $directory = $_SERVER["DOCUMENT_ROOT"]."/cron/";
    $file = "register_warranty_". date("dMY") .".csv";
    $filepath = $directory.$file;

    if(!file_exists($filepath)) 
    {
        $fp = fopen($filepath, "w+");
        fwrite($fp, "Title, Initials, Surname, HomeTel, Mobile, HouseNumber, Address, Address2, Town, Postcode, NewlyBuiltHome, InstallationDate, Houseowner, InstallersName, InstallersEmail, InstallersMobile, InstallersGasSafeNumber, BusinessName, BusinessAddress, BusinessAddress2, BusinessTown, BusinessRegion, BusinessPostcode, BusinessGasSafeNumber, BusinessEmail, SuppliersName, Model, serial, MaintenanceContract, ExpiryDate, Donotwishtobecontactedviapost, Donotwishtobecontactedviatelephone, Donotwishtobecontactedviaemail, Donotwishtobecontactedviasms\n");
        fclose($fp);
    }

?>

在运行 cron 时收到的 Cron 电子邮件中 ​​- 我收到以下错误,

第 1 行:?php:没有这样的文件或目录
第 3 行:意外标记附近的语法错误 ('
line 3:
$_SERVER['DOCUMENT_ROOT'] = dirname(FILE);'

我是不是在做一些明显错误的事情,我在 Cron 工作方面不是最有经验的人。

【问题讨论】:

    标签: php apache cron


    【解决方案1】:

    你可以做两件事之一:

    1. crontab 执行您的 PHP 脚本:

      /usr/bin/php -q script.php

    2. 在 PHP 脚本的开头添加 shebang 行:

      #!/usr/bin/php -q

      并为您的脚本授予执行权限:

      chmod +x script.php

    【讨论】:

      【解决方案2】:

      我想你忘记在 cronjob 中的文件名前添加 php 命令了。

      php myscript.php

      【讨论】:

      • /usr/bin/php -f myscript.php 应该使用而不是 php myscript.php [/usr/bin/ 是执行 php 的示例路径,以查找您的.. 输入shell which php]
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 1970-01-01
      • 2017-07-04
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      相关资源
      最近更新 更多