我在CentOS 6.4(最终)服务器(安装了PHP 5.5.9和Apache httpd 2.4.4)上设置了一个cronjob:
30 15 * * * wget "http://10.15.1.2/calc.php" -O /dev/null
使用PHP内置的ftp函数将多个服最后将计数结果插入另一个汇总表。这是一个非常简单的程序。
calc.php 从15:30开始,到15:46结束(calc.php会将时间写入日志)。当我检查日志时,我发现calc.php正在15:45再次执行。(几乎是在第一次运行时接近结束。)我已经仔细检查了我的calc.php。在主逻辑中,我没有使用任何循环语句,例如while,do-while或for等。上面提到的所有任务都作为函数写在同一个程序文件中。我在浏览器上多次运行相同的URL,它们都正常工作。
那么在cron作业中运行时可能导致重复执行的原因是什么?
以下是主要逻辑部分(注意: myxxxx()是我自己的简单显示消息功能。 USEARRAY_MODE 和 TESTINSTMP_MODE 在运行时未定义):
myassess("Being-calcuated logfile list=\n".xpr($srclog_fn_list));
if (defined('USEARRAY_MODE')) {
// Insert outer srclog into temp array
if (false === ($srclog_calc_date = insert_srclog_to_array($srclog_get_date, $srclog_dir, $srclog_delta_days, $srclog_fn_list, $logtmp_sfile, $logtmp_dfile, $logtmp_result))) {
myerror("Insert outer srclog to temp array has problem!");
exit;
}
myassess("Actually calculated date = $srclog_calc_date.");
// If testinstemp mode, we stop here without doing statistics
if (defined('TESTINSTMP_MODE')) {
myinfo("Skipping statistics in testinstmp mode.");
exit;
}
// Do statistics and then insert into summary table
$total_cnt_list = do_statistics_from_array($_cnt_g2s_array, $userlist_tempfile, $rule_file, $srclog_calc_date, $logtmp_result);
} else { // use table
// Insert outer srclog 入 temp array
if (false === ($srclog_calc_date = insert_srclog_to_table($srclog_get_date, $srclog_dir, $srclog_delta_days, $srclog_fn_list))) {
myerror("Insert outer srclog to temp table has problem!");
exit;
}
myassess("Actually calculated date = $srclog_calc_date.");
// If testinstemp mode, we stop here without doing statistics
if (defined('TESTINSTMP_MODE')) {
myinfo("Skipping statistics in testinstmp mode.");
exit;
}
// Do statistics and then insert into summary table
$total_cnt_list = do_statistics_from_table($_cnt_g2s_array, $userlist_tempfile, $rule_file, $srclog_calc_date);
}
if (false === $total_cnt_list)
myerror("Calculate/Write outer summary has problem!");
else {
myinfo("Outer srclog actually calculated date = $srclog_calc_date.");
myinfo("Total summary count = ".array_sum($total_cnt_list));
myinfo("Insert-ok summary count = ".$total_cnt_list[0]);
myinfo("!!!Insert-fail summary count = ".$total_cnt_list[1]);
}
@mysql_close($wk_dbconn);
@oci_close($uodb_conn);
myinfo("### Running end at ".date(LOG_DATEFORMAT1).".");
myinfo("### total exectution time:".elapsed_time($_my_start_time, microtime(true)));
myinfo("############ END program ############");
exit;