doubei3312 2014-08-27 19:46
浏览 51
已采纳

通过cron运行php脚本。 如何让它报告失败?

I have a PHP script that I want to run on a cronjob. I am assuming if PHP fails due to a fatal error it will terminate the script which reports to cron of an error, however how do I force a failure reply that will instruct cron of an issue?

I will have some database calls and I want cron to report back an error (which fires off an email) if the database queries fail.

  • 写回答

1条回答 默认 最新

  • drl959975 2014-08-27 20:12
    关注

    Here is what I do:

    $query1 = "some query";
    $results = mysql_query($query1);
    $query1Error = mysql_error();
    
    $query2 = "a different query";
    $results = mysql_query($query2);
    $query2Error = mysql_error();
    
    if(!empty($query1Error) || !empty($query2Error) ) {
            $message = "An error occured updating the funnel. The details are below:"."
    
    ";
            $message = print_r($_POST,TRUE);
            $message .= "
    
    Query 1: ".$query1;
            $message.="
    
     Query 1 Error: ".$query1Error; 
            $message .= "
    
    Query 2: ".$query2;
            $message.="
    
     Query 2 Error: ".$query2Error; 
            mail("someone@somewhere.com","Error updating funnel ".__FILE__." ",$message);  
       }
    

    Of course, when i get around to it, I will replace the depreciated MySQL stuff.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?