我正在尝试在我的Linux服务器上设置一个cron作业。
我有以下代码
*/1 * * * * root php -f /var/www/html/includes/cron/cron.inc.a.php &> dev/php.log
cron作业要调用的文件,从另一台服务器获取心跳响应。
<?php
$l = new MySQLi();
$l->connect(blah);
$q = $l->query("SELECT * FROM `tbl_latency_stats`");
set_include_path('/var/www/html/includes/');
include("ping/ping.php");
$host = '<IP ADDRESS>';
$ping = new Ping($host);
$latency = $ping->ping();
if (!is_bool($latency)){
if(is_float($latency) || is_string($latency)) {
if(is_string($latency)){
$s = split(".", $latency);
if($s[0] >= 100){
$response_array['status'] = "success";
$response_array['state'] = "WARNING";
$response_array['timedate'] = date("Y-m-d H:i:s");
$response_array['response'] = "High Latency";
$response_array['ping'] = "$latency";
$response_array['message'] = "null";
}else{
$response_array['status'] = "success";
$response_array['state'] = "UP";
$response_array['timedate'] = date("Y-m-d H:i:s");
$response_array['response'] = "LIVE";
$response_array['ping'] = "$latency";
$response_array['message'] = "null";
}
}elseif(is_float($latency)){
if($latency >= 100){
$response_array['status'] = "success";
$response_array['state'] = "WARNING";
$response_array['timedate'] = date("Y-m-d H:i:s");
$response_array['response'] = "High Latency";
$response_array['ping'] = "$latency";
$response_array['message'] = "null";
}else{
$response_array['status'] = "success";
$response_array['state'] = "UP";
$response_array['timedate'] = date("Y-m-d H:i:s");
$response_array['response'] = "LIVE";
$response_array['ping'] = "$latency";
$response_array['message'] = "null";
}
}
}else{
$response_array['status'] = "error";
$response_array['state'] = "UNKONWN";
$response_array['timedate'] = date("Y-m-d H:i:s");
$response_array['message'] = "Unexpected Response!";
$response_array['response'] = "$latency";
$response_array['ping'] = "null";
}
}else {
$response_array['status'] = "error";
$response_array['state'] = "DOWN";
$response_array['timedate'] = date("Y-m-d H:i:s");
$response_array['message'] = "Request Unreachable!";
$response_array['response'] = "$latency";
$response_array['ping'] = "null";
}
$q = $l->query("INSERT INTO `enigmaStat`.`tbl_latency_stats` (`latency_stat_id` ,`latency_stat_date_time` ,`latency_stat_status` ,`latency_stat_response` ,`latency_stat_latency_value` ,`latency_stat_user_text`)VALUES ('' , '".$response_array['timedate']."' , '".$response_array['state']."', '".$response_array['response']."', '".$response_array['ping']."', '".$response_array['message']."')");
?>
但没有插入。我已从网站路径运行该文件,并通过命令行手动运行,如下所示:
php -f /var/www/html/includes/cron/cron.inc.a.php
并将其插入数据库。
我是服务器配置等的新手,所以如果不够详细,我很抱歉,我很乐意添加任何必要的细节来帮助解决问题。
感谢。
修改
关注维基,我尝试过:
env -i sh -c 'php -f /var/www/html/includes/cron/cron.inc.a.php'
它可以工作并插入数据库。
答案 0 :(得分:1)
PHP cron作业有时需要可选标志:
*/1 * * * * php -q /var/www/html/includes/cron/cron.inc.a.php &> dev/php.log