2013-10-17 92 views
0

我有一個Codeigniter控制器,它在調用時通過exec啓動另一個腳本。一切工作在Ubuntu下與PHP加載爲mod_php。當我將我的應用程序上傳到我的生產服務器時,腳本被我的應用程序啓動,但調用原始控制器而不是預期的控制器。我猜測,Apache/CGI仍然有我舊的環境變量?APACHE上的Codeigniter PHP FastCGI

下面的代碼:

public function onshore_xml_queue_report() {   

    if(!$this->tank_auth->is_logged_in()) { 
     log_message('error', "Not logged in onshore_xml_queue_report"); 
     show_error("Not logged in"); 
    } 
    /* 
    else if(ENVIRONMENT == "production" && HOMEACCOUNT != "report") { 
     log_message('error', "Invalid account access onshore_xml_queue_report: " . HOMEACCOUNT); 
     show_error("Invalid Account Access");  
    } 
    */ 
    else { 

     // get report parameters       
     $this->form_validation->set_rules('year', 'Year', 'numeric|required|xss_clean'); 
     $this->form_validation->set_rules('company', 'Company', 'required|xss_clean'); 
     $this->form_validation->set_rules('analysis', 'Analysis', 'required|xss_clean'); 
     $this->form_validation->set_rules('constants', 'Constants', 'required|xss_clean'); 
     $this->form_validation->set_rules('basin', 'Basin ID', 'required|xss_clean'); 
     $this->form_validation->set_rules('email', 'Email', 'required|xss_clean'); 
     $this->form_validation->set_rules('user', 'User ID', 'required|xss_clean'); 

     // check if we have all parameters   
     if($this->form_validation->run() != FALSE) { 

      $this->output->set_content_type('application/json'); 

      // post input parameters 
      $year = intval($this->input->post('year', TRUE)); 
      $company = $this->input->post('company', TRUE); 
      $analysis = intval($this->input->post('analysis', TRUE)); 
      $constants = $this->input->post('constants', TRUE); 
      $basin = intval($this->input->post('basin', TRUE)); 
      $email = intval($this->input->post('email', TRUE)); 
      $user = intval($this->input->post('user', TRUE)); 

      // lock queue 
      $this->Queue_Model->lock("queueGhgOnshoreXML"); 

      // check if any reports are currently being processed 
      $queue = $this->Queue_Model->getQueue(); 
      $bQueueActive = FALSE; 
      foreach($queue as $entry) { 
       if($entry["processingStatus"] == 1) { 
        $bQueueActive = TRUE; 
        break; 
       } 
      } 

      log_message('debug', "Report controller queue status $bQueueActive"); 

      // enqueue report 
      $reportId = $this->Queue_Model->enqueue(array(
        "year" => $year, 
        "companyName" => $company, 
        "facilityId" => $basin, 
        "analysis" => $analysis, 
        "constants" => $constants, 
        "userId" => $user, 
        "email" => $email 
      )); 

      // if we are not currently processing, start report script via background command line 
      if(!$bQueueActive) { 

       if(count($queue) == 0) 
        $queue = $this->Queue_Model->getQueue(); 

       // FIFO queue, get earliest report id 
       $queueLength = count($queue); 
       $earliestReportId = $queue[$queueLength-1]["id"]; 

       log_message('debug', "Report controller kicking off processing script $earliestReportId"); 

       // update report record to show that we are processing 
       $this->Queue_Model->updateEntry($earliestReportId, array("processingStatus" => 1)); 

       // append any output to debug file, should never be output unless serious PHP error 
       $logFile = $this->config->item('log_path') . "onshore_xml_create_report_error.txt"; 

       log_message('debug', "Report controller logFile $logFile"); 

       $command = "nohup php index.php report onshore_xml_create_report > $logFile 2>&1 &"; 

       // 2>>&1 - causes error 
       // $command = "ls -l >> $logFile 2>&1 &";  // this works on Hostgator... 

       // http://php.net/manual/en/function.shell-exec.php 
       $output = exec($command); 

       log_message('debug', "Report controller command $command output $output"); 
      } 

      // unlock reports table 
      $this->Queue_Model->unlock(); 

      log_message('debug', "Report controller unlocked queue new report id $reportId"); 

      // return report id 
      $this->output->set_output(json_encode(array(
       "success" => true, 
       "reportId" => $reportId 
      ))); 
     } 
     else { 
      show_error("onshore_xml_queue_report: Missing Parameters"); 
     }   
    } 
} 

public function onshore_xml_create_report() { 

    log_message('debug', 'Starting onshore_xml_create_report'); 

再次onshore_xml_create_report函數被調用我的本地機器上,並onshore_xml_queue_report被稱爲生產服務器上。

回答

1

PHP有一個函數php_sapi_name,Codeigniter在系統URI類中使用它來確定請求是命令行還是Web請求。在Apache下使用PHP FastCGI啓用調用任何系統,shell_exec,exec,popen或創建調用php解釋器的新進程的相關函數,除非使用了爲CLI編譯的特定版本的PHP,否則它仍會使用父進程的環境變量。

簡而言之,/ usr/bin/php可能是FastCGI編譯器,因此您必須使用/ usr/bin/php-cli。

這是一個相關的Stackoverflow後PHP CGI replace...