我有一个以cron身份运行的PHP脚本。抛出异常时,它会执行catch块然后死掉。如果我在cli中运行相同的脚本,而不是作为cron作业,它将运行catch块并继续运行脚本的其余部分。知道为什么会这样吗?
以下是引发异常的地方:
public static function concurrentGet($urlList, $callback, $passthrough = null) {
//init the guzzle client
$client = new Client([
"headers" => [
"Accept" => "text/html,text/json,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding" => "gzip, deflate, sdch",
"Accept-Language" => "en-US",
"Cache-Control" => "no-cache",
"Connection" => "keep-alive",
"Pragma" => "no-cache",
"User-Agent" => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"
],
'debug' => false,
"verify" => false,
"cookies" => true
]);
//this function is called by the pool to generate request objects
$requests = function ($urlList) {
foreach ($urlList as $url) {
yield new Request('GET', $url);
}
};
//execute the pool of requests
$pool = new Pool($client, $requests($urlList), [
'concurrency' => 5,
'fulfilled' => function ($response, $index) use ($urlList, $callback, $passthrough) {
$url = $urlList[$index];
//$app['logger']->info("GET: $url...");
$body = $response->getBody()->getContents();
//call passed callback
call_user_func(
$callback,
$body,
$url,
$passthrough
);
},
'rejected' => function (\Exception $reason, $index) use ($urlList, $callback, $passthrough) {
$url = $urlList[$index];
echo "REJECT: ".$url."\n";
//echo "REASON: ".$reason."\n";
throw $reason;
},
]);
以下是它的捕获方式:
try {
$report->concurrentGet($report->OSRMRequests, [$report, 'OSRMCallback']);
} catch (\Exception $e) {
//send to airbrake
Airbrake\Instance::notify($e);
//log to internal application log
$app['errorLog']->error($e->getMessage());
}
*/1 * * * * /usr/bin/php ~/Projects/easyivmr/bin/console createreport >> /var/log/ivmr_report.log
我正在运行Ubuntu 16.04.03 LTS。