<?php
include_once 'bu-common.php';
function btn_run_bu( $buid, $testrun='true' ){
//we cannot fork using php inside apache for some stupid reason.
//so we need to deligate a fork to an external process.
//BASH can do this with the background '&' command, so we use it.
global $g_phpcli_path;
$output = array();
$result_code = null;
$bIsTest = $testrun=='true';
if(!$bIsTest) $str_test = '0 &';
else $str_test = '1';
// get realpath, cos this is what is specified in visudo:
//www-data ALL=(ALL) NOPASSWD: /usr/bin/php /var/www/html/bin/run-bu-by-id.php *
$path_to_script = realpath(__DIR__."/../bin/run-bu-by-id.php");
$command = "sudo $g_phpcli_path $path_to_script $buid $str_test 2>&1";
//error_log("btn_run_bu to execute cmd: $command");
if( exec( $command, $output, $result_code )===FALSE){
error_log( "exec to php-cli failed - command: $command");
return array('error', "exec to php-cli failed - command: $command" );
}
//error_log( print_r($output,true) );
//even though the script ran, it may have echoed errors to the console. We test here.
if( count($output)>0 ){
if( substr( $output[0], 0, 6) == 'Error:' ){
error_log( $output[0] );
return array('error', $output[0] );
}
}
if($testrun!='false'){
//a test run waits for the command to finish, so the output can be collected and sent back now
for($i=0;$i<2;$i++){
$fn = __DIR__."/../data/".getFilename_out( $buid, $bIsTest, $i==0 );
$output[] = file_get_contents($fn);
}
return array_merge( array('done'), $output );
}
//error_log( "Backup kicked off for ID($buid): isTest=$isTest");
return array_merge( array('running'), $output );
}
?>