用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2824|回复: 2
收起左侧

[库 Library] crontab 脚本

[复制链接]
发表于 2012-8-7 10:57:23 | 显示全部楼层 |阅读模式
一直在用这个脚本写一些crontab的处理,比如定时读取数据库的数据并更新。
PHP复制代码
 
<?php
/**
 *
 * @package crontab
 * @desc crontab 脚本基础
 * @version v1.0
 * @author gray.liu[gaoomei@gmail.com]
 * @since 2012-07-18
 * @link http://obatu.sinaapp.com
 */

date_default_timezone_set ( 'Asia/Shanghai' );
if (! defined ( 'BASEPATH' )) {
        define ( 'BASEPATH', dirname ( __FILE__ ));
}
 
class CrontabBase {
        /**
         * @var string
         */

        private $pid_file_name;
        /**
         * @var resource
         */

        private $pid_file_handle;
        /**
         * @var string
         */

        private $log_dir = 'logs';
        /**
         * @var string
         */

        private $log_file;
       
        /**
         * 构造函数
         */

        public function __construct() {
                register_shutdown_function ( array ($this, "stop" ) );
                $this->log_dir = BASEPATH.$this->log_dir;
                if(!is_dir($this->log_dir)){
                        mkdir($this->log_dir,0775);
                }
                $this->pid_file_name = $this->log_dir.'/crontab-pid-' . date ( 'Ymd-H' ) . '.pid';
                $this->log_file = $this->log_dir.date('Y-m-d').'.log';
        }
       
        /**
         * 脚本入口
         */

        public function start() {
                set_time_limit ( 0 );
                ignore_user_abort ( true );
                $this->log ( 'start' . PHP_EOL );
                if (file_exists ( $this->pid_file_name )) {
                        $this->log ( 'anthoer script was start,go to stop!' . PHP_EOL );
                        return;
                }
                $this->pid_file_handle = fopen ( $this->pid_file_name, 'w+' );
                if (flock ( $this->pid_file_handle, LOCK_EX )) {
                        $this->log ( 'flock file: ' . $this->pid_file_name . PHP_EOL );
                        fwrite ( $this->pid_file_handle, getmypid ());
                        $this->handler ();
                }
        }
       
        /**
         * 脚本停止
         */

        public function stop() {
                if (is_resource ( $this->pid_file_handle )) {
                        @flock ( $this->pid_file_handle, LOCK_UN );
                        @fclose ( $this->pid_file_handle );
                }
                if (file_exists ( $this->pid_file_name )) {
                        @unlink ( $this->pid_file_name );
                }
               
                $error = error_get_last ();
                if (! empty ( $error ) && ($error ['type'] ^ E_NOTICE)) {
                        $msg = "error_type:" . $this->get_error_type ( $error ['type'] ) . ',message:' . $error ['message'] . ',file:' . $error ['file'] . ' ' . $error ['line'];
                        $this->log ( $msg );
                }
               
        }
       
        /**
         * 业务处理
         */

        protected function handler() {
               
        }
       
       
        /**
         * 记录log信息
         * @param string $msg
         */

        protected function log($msg) {
                $datetime = date ( '[Y-m-d H:i:s]' );
                $msg = $datetime.$msg.PHP_EOL;
                @file_put_contents ( $this->log_file, $msg, FILE_APPEND );
        }
       
        /**
         * 获取错误类别
         * @param int $errType
         */

        protected function get_error_type($errType) {
                $errortype = array (E_ERROR => 'Error', E_WARNING => 'Warning', E_PARSE => 'Parsing Error', E_NOTICE => 'Notice', E_CORE_ERROR => 'Core Error', E_CORE_WARNING => 'Core Warning', E_COMPILE_ERROR => 'Compile Error', E_COMPILE_WARNING => 'Compile Warning', E_USER_ERROR => 'User Error', E_USER_WARNING => 'User Warning', E_USER_NOTICE => 'User Notice', E_STRICT => 'Runtime Notice', E_RECOVERABLE_ERROR => 'Catchable Fatal Error' );
                if (! in_array ( $errType, $errortype )) {
                        return $errType;
                }
                return $errortype [$errType];
        }
}
/*
$crontab = new CrontabBase ();
$crontab->start ();
*/

?>
 
 
复制代码

发表于 2012-8-15 17:18:26 | 显示全部楼层
请教下 详细的使用方式...谢谢!{:soso_e183:}
 楼主| 发表于 2012-8-16 09:16:01 | 显示全部楼层
终结者 发表于 2012-8-15 17:18
请教下 详细的使用方式...谢谢!

就是定时执行脚本

本版积分规则