有时在cron作业中使用脚本失败

时间:2014-07-04 08:27:25

标签: php cron

我有这个cron命令:

php <path>/export.php

每天午夜跑步。我知道命令本身是因为我期望创建的文件被触发,但是只填充了客户文件。这是脚本:

<?php
//============================================================


//- CRON TO BE RUN EVERY DAY 23:59


//============================================================

// be massively careful if you run this on review! 
$sitePath='<removed>';

require_once($sitePath.'<removed>');
set_time_limit(240);

$sage = new Sage();

    $transactionCSV = '';
#-only needed for opening balances
#   $transactionCSV .= $sage->obDebt();
#   $transactionCSV .= $sage->obCredit();
#-

    $transactionCSV .= $sage->ordersCsv();
    $transactionCSV .= $sage->paymentsCsv();
    $transactionCSV .= $sage->creditsCsv();
    $transactionCSV .= $sage->refundsCsv();
        $adj = nl2br(file_get_contents($sitePath.'<removed>'));
    $transactionCSV .= str_replace('<br />', "\r\n", $adj);

    $customerCSV = $sage->customersCsv();

    @file_put_contents($sitePath.'<removed>', '');

    $msg='';
    //var_dump($transactionCSV);
    //exit();

    file_put_contents($sitePath.'<removed>'.date('Y-m-d').'.csv', $customerCSV);    
    file_put_contents($sitePath.'<removed>'.date('Y-m-d').'.csv', $transactionCSV);

    #delete old automated files
    @unlink($sitePath.'<removed>');
    @unlink($sitePath.'<removed>');

    #put in location available via FTP (readonly) as root
    touch($sitePath.'<removed>');
    touch($sitePath.'<removed>');

    file_put_contents($sitePath.'<removed>', $customerCSV); 
    file_put_contents($sitePath.'<removed>', $transactionCSV);

    #allow wood-sage to delete files
    chmod($sitePath.'<removed>', 0777);
    chmod($sitePath.'<removed>', 0777);


    $mail = new PHPMailer();
    $mail->IsSMTP();                                   // send via SMTP
    $mail->Host     = $mailServer; // SMTP servers
    $mail->SMTPAuth = true;     // turn on SMTP authentication
    $mail->SMTPDebug = false; //toggle debugging mode true / false
    $mail->Username = $mailBox;  // SMTP username
    $mail->Password = $mailPass; // SMTP password
    $mail->From     = '<removed>';
    $mail->FromName = '<removed>';

    $mail->AddAddress('<removed>', '<removed>');
    $mail->AddCc('<removed>', '<removed>');
    $mail->AddCc('<removed>', '<removed>');
    //for debug purposes
    $mail->AddBcc($webEmail, 'Webmaster');

    $mail->Subject = 'Sage Export: '.date('d/m/Y');

    if(filesize($sitePath.'<removed>'.date('Y-m-d').'.csv')>0){
        $mail->AddAttachment($sitePath.'<removed>'.date('Y-m-d').'.csv');
    }else{
        $msg.='Customer export contains no data.'.chr(10).chr(13);
    }

    if(filesize($sitePath.'<removed>'.date('Y-m-d').'.csv')>0){
        $mail->AddAttachment($sitePath.'<removed>'.date('Y-m-d').'.csv');
    }else{
        $msg.='Transaction export contains no data.';
    }

    $mail->Body=$msg;

    $mail->Send();

exit();

?>

当我通过浏览器手动触发脚本时,一切正常。当我在托管控制面板中使用fire manual命令时,一切正常。当我将cron作业设置为在几分钟内自动启动时,一切正常。

事实上,我可以找到打破脚本的唯一方法是将命令放在正常的午夜设​​置上 - 而且我完全不知道为什么。为了记录,最近错过的cron作业返回了10行的客户文件和499的交易文件,我发现很难相信很少会导致超时或类似的错误。

编辑:

根据评论中的建议,我通过SSH解雇了命令,并收到以下警告:

PHP Warning:  file_put_contents(<removed>): failed to open stream: Permission denied in <removed> on line 39
PHP Warning:  file_put_contents(<removed>): failed to open stream: Permission denied in <removed> on line 40
PHP Warning:  Unknown: open(/var/lib/php/session/sess_lnf9i3q43mrsosmt1dfd17el04, O_RDWR) failed: Permission denied (13) in Unknown on line 0
PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php/session) in Unknown on line 0

显然前两个与不正确的文件权限有关,但我不知道其他两个是什么。

0 个答案:

没有答案