Reputation: 512
I have a Codeigniter controller that when called kicks off another script via exec. Everything works under Ubuntu with PHP loaded as mod_php. When I upload my app to my production server the script is kicked off by my app but the original controller is called instead of the intended controller. I'm guessing that somehow Apache / CGI still has my old environment variables?
Here's the code:
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');
Again the onshore_xml_create_report function is called on my local machine and on the production server onshore_xml_queue_report is called.
Upvotes: 0
Views: 1450
Reputation: 512
PHP has a function php_sapi_name that Codeigniter uses in the system URI class to determine whether the request is a command line or web request. Under Apache with PHP FastCGI enabled calling any of the system, shell_exec, exec, popen or related functions that create a new process that invokes the php interpreter will still utilize the parent processes' environment variables unless a specific version of PHP compiled for CLI is used.
In a nutshell /usr/bin/php may be a FastCGI compilation so you must use /usr/bin/php-cli.
This is a related Stackoverflow post PHP CGI replace...
Upvotes: 1