我有一个使用php exec()函数运行命令的脚本。它在linux中运行良好,但在OSX中运行不正常
脚本很大,所以我只显示相关代码:
sim.php:
$rootDir = dirname(__DIR__);
$simulation_id = mysql_insert_id();
exec("cd $rootDir; nohup php run_simulation.php $simulation_id 2> /tmp/errorphp.txt");
run_simulation.php
include(dirname(__FILE__) . "/include.php");
$simulation_id = $argv[1];
$get_simulation_row = mysql_fetch_array(mysql_query("select * from `_simulations` where `id` = $simulation_id"));
..... rest of code
这在linux上工作正常。但是,当它在OSX上时,脚本会在include处停止执行。
我知道这是因为如果我在包含之前输入错误日志字符串输出到' /tmp/errorphp.txt'如果我在包含后没有输出字符错误日志。
run_simulation.php
error_log('This string shows in log file');
$simulation_id = $argv[1];
error_log('so does this');
include(dirname(__FILE__) . "/include.php");
error_log('This and anything after does not show in log file');
注意:我知道我应该使用PDO而不是mysql()但这不是我的代码我正在为某人编辑它。我将来会为他们链接PDO
更新
chris.php for chris
<?php
$link = mysql_connect('localhost', 'user', 'password');
if (!$link) {
die('DB error: ' . mysql_error());
}
mysql_select_db('rates');
mysql_query("SET NAMES 'utf8'");
$currentLine;
$testLine;
$patternAr = array();
$performanceAr = array();
$pointsToLookAt = 30;
$forwardPoints = 10;
$patForRec = array();
function avgRange($range) {
if (count($range) == 0) {
return 0;
}
$close = 0;
for ($i = 0; $i < count($range); $i++) {
$close += $range[$i]["close"];
}
$close = $close/count($range);
return $close;
}
function highRange($range) {
$high = -100000000;
for ($i = 0; $i < count($range); $i++) {
$close = $range[$i]["close"];
if ($close > $high) $high = $close;
}
return $high;
}
function lowRange($range) {
$low = 100000000;
for ($i = 0; $i < count($range); $i++) {
$close = $range[$i]["close"];
if ($close < $low) $low = $close;
}
return $low;
}
function storePatterns($model_id, $exclude_hours = array()) {
$time_start = microtime(true);
global $currentLine;
global $patternAr;
global $performanceAr;
global $pointsToLookAt;
global $forwardPoints;
$x = count($currentLine) - ($pointsToLookAt*2);
$y = $pointsToLookAt + 1;
while ($y < $x) {
// to exclude hours
if (count($exclude_hours) > 0) {
$c_date = date_create_from_format("Y-m-d H:i:s", $currentLine[$y]["date"]);
$c_date = date_format($c_date, "H");
if (in_array($c_date, $exclude_hours)) {
echo "Excluding hour: $c_date \n";
$y++;
continue;
}
}
$pattern = array();
for ($i = $pointsToLookAt-1; $i >=0; $i--) {
array_push($pattern, percentChange(avgLine($y-$pointsToLookAt), avgLine($y-$i)));
}
$outcomeRange = array_slice($currentLine, $y+1, $forwardPoints);
$currentPoint = avgLine($y);
$avgOutcome = avgRange($outcomeRange);
$highOutcome = percentChange($currentPoint, highRange($outcomeRange));
$lowOutcome = percentChange($currentPoint, lowRange($outcomeRange));
$futureOutcome = percentChange($currentPoint, $avgOutcome);
$outcomes = array(
"high" => $highOutcome,
"low" => $lowOutcome,
"avg" => $futureOutcome
);
array_push($patternAr, $pattern);
array_push($performanceAr, $outcomes);
$y++;
$currTime = microtime(true);
$time_in = ($currTime - $time_start);
//if ($time_in % 10 == 0) {
//if (round(($y/($x-1))*100) != 0) {
if ($y%100 == 0) {
echo "Pattern progress: ".round(($y/($x-1))*100, 2)."%\n";
updateModelProgress($model_id, round(($y/($x-1))*100, 2)."%");
}
//}
//}
}
updateModelProgress($model_id, "100%");
$models_file = array("patterns" => $patternAr, "outcomes" => $performanceAr);
$models_file = json_encode($models_file);
file_put_contents("models/$model_id".".model", $models_file);
$time_end = microtime(true);
$execution_time = ($time_end - $time_start)/60;
//echo "Execution time $execution_time mins\n";
}
function updateModelProgress($model_id, $progress) {
mysql_query("update `_models` set `progress` = '$progress' where `id` = $model_id");
}
function updateModelStatus($model_id, $status) {
mysql_query("update `_models` set `status` = '$status' where `id` = $model_id");
}
function updateSimulationProgress($simulation_id, $progress) {
mysql_query("update `_simulations` set `progress` = '$progress' where `id` = $simulation_id");
}
function updateSimulationStatus($simulation_id, $status) {
mysql_query("update `_simulations` set `status` = '$status' where `id` = $simulation_id");
}
function updateSimulationDescription($simulation_id, $desc) {
mysql_query("update `_simulations` set `description` = '$desc' where `id` = $simulation_id");
}
function currentPattern($upto) {
global $patForRec;
global $pointsToLookAt;
global $testLine;
$patForRec = array();
$line_upto = array_slice($testLine, 0, $upto);
for ($i = $pointsToLookAt; $i > 0; $i--) {
array_push($patForRec, percentChange(avgTestLine(-$pointsToLookAt-1, $line_upto), avgTestLine(-$i, $line_upto)));
}
}
function patternRecognition($upto, $minSimilarity) {
global $patternAr;
global $pointsToLookAt;
global $patForRec;
global $forwardPoints;
global $testLine;
global $performanceAr;
$predictedOutcomesAr = array();
$patFound = 0;
$plotPatAr = array();
$plotPatIndex = array();
$p = 0;
foreach ($patternAr as $pattern) {
$simAr = array();
$sim_bool = true;
for ($i =0; $i < $pointsToLookAt; $i++) {
$sim_num = 100.0-abs(percentChange($pattern[$pointsToLookAt-$i-1], $patForRec[$pointsToLookAt-$i-1]));
if ($sim_num < 0) {
$sim_bool = false;
break;
}
array_push($simAr, $sim_num);
}
if ($sim_bool) {
$howSim = array_sum($simAr)/count($simAr);
if ($howSim > $minSimilarity) {
array_push($plotPatAr, $pattern);
array_push($plotPatIndex, $p);
$patFound = 1;
}
}
$p++;
}
$predArray = array();
$returnArray = array();
$returnArray["prediction"] = "none";
if ($patFound == 1) {
$p = 0;
foreach ($plotPatAr as $pattern) {
$futurePoints = $plotPatIndex[$p];
// print_r($performanceAr[$futurePoints]);
// print_r($patForRec[$pointsToLookAt-1]);
// if ($performanceAr[$futurePoints]["avg"] > $patForRec[$pointsToLookAt-1]) {
if ($performanceAr[$futurePoints]["avg"] > $pattern[$pointsToLookAt-1]) {
array_push($predArray, 1.0);
} else {
array_push($predArray, -1.0);
}
array_push($predictedOutcomesAr, $performanceAr[$futurePoints]);
$p++;
}
$realOutcomeRange = array_slice($testLine, $upto, $forwardPoints);
$realOutcomeAvg = avgRange($realOutcomeRange);
$realMovement = percentChange($testLine[$upto]["close"], $realOutcomeAvg);
$predictedAvgOutcome = array_sum($predictedOutcomesAr)/count($predictedOutcomesAr);
if (count($predArray) > 0) {
// print_r($predArray);
}
$predictionAvg = array_sum($predArray)/count($predArray);
if ($predictionAvg < 0) {
// echo "";
$returnArray["prediction"] = "sell";
$returnArray["pAvg"] = $predictionAvg;
}
if ($predictionAvg > 0) {
$returnArray["prediction"] = "buy";
$returnArray["pAvg"] = $predictionAvg;
}
}
$returnArray["patterns"] = $plotPatAr;
$returnArray["original"] = $patForRec;
$returnArray["outcomes"] = $predictedOutcomesAr;
return $returnArray;
}
function percentChange($startPoint, $currentPoint) {
$currentPoint = $currentPoint*1.0;
try {
$x = (($currentPoint-$startPoint)/abs($startPoint))*100.0;
if ($x == 0) {
return 0.0000000001;
} else {
return $x;
}
} catch (Exception $e) {
return 0.0000000001;
}
}
function avgLine($row) {
global $currentLine;
if ($row < 0) {
$row = count($currentLine)+$row;
}
return $currentLine[$row]["close"];
}
function avgTestLine($row, $line) {
if ($row < 0) {
$row = count($line)+$row;
}
return $line[$row]["close"];
}
function getRowsFromDatabase($query) {
$array = array();
while ($row = mysql_fetch_array($query)) {
array_push($array, $row);
}
return $array;
}
function getData($instrument, $from, $to, $interval='m') {
global $currentLine;
$groupby = "YEAR(`date`), MONTH(`date`), DAY(`date`), HOUR(`date`), MINUTE(`date`)";
$interval = strtolower($interval);
if ($interval == 'm' || $interval == 'minute' || $interval == 'minutes') {
$groupby = "YEAR(`date`), MONTH(`date`), DAY(`date`), HOUR(`date`), MINUTE(`date`)";
} else if ($interval == 'h' || $interval == 'hour' || $interval == 'hours') {
$groupby = "YEAR(`date`), MONTH(`date`), DAY(`date`), HOUR(`date`)";
}
$instrument = strtoupper($instrument);
$instrument = str_replace("/", "", $instrument);
$query = mysql_query("select date, TRUNCATE(MAX((buy+sell)/2), 5) as high, TRUNCATE(MIN((buy+sell)/2), 5) as low, MIN(id) as open, MAX(id) as close, MAX(id) as close_id from `$instrument` where `date` > '$from' and `date` < '$to' group by $groupby order by `date`");
$array = array();
while ($row = mysql_fetch_array($query)) {
$open_row = mysql_fetch_array(mysql_query("select TRUNCATE(((buy+sell)/2), 5) as open from `$instrument` where `id` = ".$row["open"]));
$close_row = mysql_fetch_array(mysql_query("select TRUNCATE(((buy+sell)/2), 5) as close from `$instrument` where `id` = ".$row["close"]));
$row["open"] = $open_row["open"];
$row["close"] = $close_row["close"];
array_push($array, $row);
}
$rows = $array;
return $rows;
}
function getTick($instrument, $tick_id) {
$instrument = strtoupper($instrument);
$instrument = str_replace("/", "", $instrument);
$query = mysql_query("select * from `$instrument` where `id` = $tick_id");
return mysql_fetch_array($query);
}
//print_r($performanceAr);
//print_r($patternAr);
//print_r($currentLine);
?>