从SysCP拿出来的cron 脚本头尾,带检查上次执行结果

非常不错的脚步。在短时间cron执行脚步的时候,可以检查上次是否执行完以及是否异常,并保持异常结果。

防止代码同时多次执行带来不可意料的结果。。

<?php
/**
* This file is part of the SysCP project.
* Copyright (c) 2003-2007 the SysCP Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.syscp.org/misc/COPYING.txt
*
* @copyright  (c) the authors
* @author     Florian Lippert <flo@syscp.org>
* @author     Florian Aders <eleras@syscp.org>
* @license    GPLv2 http://files.syscp.org/misc/COPYING.txt
* @package    System
* @version    $Id: cron_init.php 1527 2008-01-31 19:47:55Z EleRas $
*/
if(@php_sapi_name() != 'cli'
   && @php_sapi_name() != 'cgi'
   && @php_sapi_name() != 'cgi-fcgi')
{
die('This script will only work in the shell.');
}
$cronscriptDebug = false; //是否打开调试,打开了不删除锁文件
$lockdir = dirname(__FILE__).'/';
$lockFilename = 'syscp_' . basename($_SERVER['PHP_SELF'], '.php') . '.lock-';
$lockfName = $lockFilename . getmypid();
$lockfile = $lockdir . $lockfName;
$keepLockFile = false; //默认值false,如果存在异常则置为true
$debugHandler = fopen($lockfile, 'w');
fwrite($debugHandler, 'Setting Lockfile to ' . $lockfile . "\n");
//打开文件锁目录查找存在的锁文件
$lockDirHandle = opendir($lockdir);
while($fName = readdir($lockDirHandle))
{
if($lockFilename == substr($fName, 0, strlen($lockFilename))
    && $lockfName != $fName)  //排除自己
{
  // Check if last run jailed out with an exception
  $croncontent = file($lockdir . $fName);
  $lastline = $croncontent[(count($croncontent)-1)];
  if($lastline == '=== Keep lockfile because of exception ===')
  {
   fclose($debugHandler);
   unlink($lockfile);
   die('Last cron jailed out with an exception. Exiting...' . "\n" . 'Take a look into the contents of ' . $lockdir . $fName . '* for more information!' . "\n");
  }
  // 检查进程是在运行还是已经假死
  $check_pid = substr(strstr($fName, "-"), 1);
  system("kill -CHLD " . $check_pid . " 1> /dev/null 2> /dev/null", $check_pid_return);
  if($check_pid_return == 1)
  {
   // 状态:       存在已经假死的进程
   // 操作:       删除它并继续执行
   fwrite($debugHandler, 'Previous cronjob didn\'t exit clean. PID: ' . $check_pid . "\n");
   fwrite($debugHandler, 'Removing lockfile: ' . $lockdir . $fName . "\n");
   unlink($lockdir . $fName);
  }
  else
  {
   // 状态         已存在正在运行的进程
   // 操作:  删除自己的锁文件退出
   fclose($debugHandler);
   unlink($lockfile);
   die('There is already a Cronjob in progress. Exiting...' . "\n" . 'Take a look into the contents of ' . $lockdir . $lockFilename . '* for more information!' . "\n");
  }
}
}
//--------开始编写代码---------------
//如果存在意外则赋值 $keepLockFile=true;

//--------结束运行代码---------------

if($keepLockFile === true)
{
fwrite($debugHandler, '=== Keep lockfile because of exception ===');
}
fclose($debugHandler);
if($keepLockFile === false
  && $cronscriptDebug === false)
{
unlink($lockfile);
}
cron_init.rar (1.47 K, 下载次数:29, 上传时间:2011-10-10 18:52)

上一篇: 同事分享的一个php生成excel的库   下一篇: 实战tomato(蕃茄)QoS设置真正实用教程

提交疑问

回顶部