<?php
ini_set("display_errors",1);
error_reporting(-1);
set_include_path('root/push');
require_once('../../var/www/includes/database.php');
require_once('pushAPNS.php');
date_default_timezone_set('Europe/London');
$departure = (string)date('H:i');
$timeNow = "$departure:00";
class Journey {}
try {
// Get all UDIDs that need to be sent a notificaiton now
$stmt = $conn->prepare("SELECT udid, bus.registration, vjc.vehicle_journey_reference, bus.line, `to`.operator_short_name AS operator, traveline.getDestinationFromJourney(vjc.vehicle_journey_reference) AS destination
FROM push_request
JOIN cron ON cron.time = :timeNow
JOIN traveline.vehicle_journey_code vjc ON vjc.vehicle_journey_reference = cron.vehicle_journey_reference
JOIN bus ON bus.journey = vjc.code
JOIN traveline.operator `to` ON `to`.operator_code = bus.operator_code
WHERE push_request.vehicle_journey_reference = vjc.vehicle_journey_reference");
$stmt->execute(array(':timeNow' => $timeNow));
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo $e->getMessage();
}
// Notify every UDID their journey is now trackable
foreach ($results as $result) {
$journey = new Journey();
$journey->operator = $result['operator'];
$journey->line = $result['line'];
$journey->departure = $departure;
$journey->destination = $result['destination'];
pushAPNS($result['udid'], $journey);
}
// Debugging
$conn->query("INSERT INTO a (`time`) VALUES ('$timeNow')");
?>
上面的代码每分钟在服务器上由cron执行一次。除了查询返回结果之外,代码每次都会到达最后一行。当脚本执行时,我似乎看不到PHP错误日志中的任何错误,任何想法我如何调试这个来解决我的问题?
我应该补充说,如果我手动执行脚本时,结果将返回脚本工作正常。只是cronjob不喜欢查询结果
Crontab: * * * * */usr/bin/php/root/push/sendpushnotifications.php
在PHP中,有两种主要的错误信息处理方式,它们之间互不依赖:
- 要显示消息,您设置display_errors
- 日志错误设置log_errors
你的代码对日志没有任何作用,它只是让它们被显示出来。因为cron在后台运行任务,所以你不会在那里看到错误。
您可以:
- 配置cron在脚本生成任何输出时发送电子邮件消息:您可以使用例如MAILTO环境变量
- 将脚本输出重定向到文件
- 配置PHP直接记录错误
试试这个
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
它也会启动其他错误…但是——找到你的命令行日志。对我来说,这是在/etc/php5/cli/php.ini
,然后搜索错误报告,并把所有的On
设置你的error_log = ./debugged_php_errors.log
或类似的东西。并不是每个错误都会在它运行的地方创建日志文件。