0

我有一个 Codeigniter 控制器,当被调用时,它会通过 exec 启动另一个脚本。一切都在 Ubuntu 下运行,PHP 加载为 mod_php。当我将我的应用程序上传到我的生产服务器时,脚本由我的应用程序启动,但调用的是原始控制器而不是预期的控制器。我猜想Apache / CGI仍然有我的旧环境变量?

这是代码:

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。

4

1 回答 1

1

PHP 有一个函数 php_sapi_name,Codeigniter 在系统 URI 类中使用该函数来确定请求是命令行还是 Web 请求。在启用 PHP FastCGI 的 Apache 下调用任何系统,shell_exec、exec、popen 或相关函数创建一个调用 php 解释器的新进程仍将使用父进程的环境变量,除非使用为 CLI 编译的特定版本的 PHP .

简而言之,/usr/bin/php 可能是 FastCGI 编译,因此您必须使用 /usr/bin/php-cli。

这是一个相关的 Stackoverflow 帖子PHP CGI 替换...

于 2013-10-18T17:35:15.750 回答