CRON作业没有执行并返回错误

时间:2012-10-31 10:14:13

标签: php apache cron

我有以下代码,我在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);
    }

&GT;

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

  

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

我做的事情显然是错的,我对Cron的工作不是最有经验的。

2 个答案:

答案 0 :(得分:1)

你可以做两件事之一:

  1. crontab执行您的PHP脚本:

    /usr/bin/php -q script.php

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

    #!/usr/bin/php -q

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

    chmod +x script.php

答案 1 :(得分:0)

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

php myscript.php