当前位置 : 主页 > 网络编程 > PHP >

检查服务器各种服务的运行状态

来源:互联网 收集:自由互联 发布时间:2021-06-30
?php/** * User: wanwan * Date: 16/2/28 * Time: 下午4:39 * email:18618300482@163.com */ date_default_timezone_set('Asia/Shanghai'); /** * 写日志 * @param $msg */function make_log($msg){ $dir = __DIR__ . '/checksLog'; if (is_dir($dir)) {
 
<?php
/**
 * User: wanwan
 * Date: 16/2/28
 * Time: 下午4:39
 * email:18618300482@163.com
 */
  
date_default_timezone_set('Asia/Shanghai');
  
/**
 * 写日志
 * @param $msg
 */
function make_log($msg)
{
    $dir = __DIR__ . '/checksLog';
    if (is_dir($dir)) {
        file_put_contents($dir . '/' . date('Y-m-d') . '.log', date("y-m-d H:i:s") . $msg . "\\n", FILE_APPEND);
    } else {
        mkdir($dir);
        file_put_contents($dir . '/' . date('Y-m-d') . '.log', date("y-m-d H:i:s") . $msg . "\\n", FILE_APPEND);
    }
}
  
/**
 * 检查进程是否存在
 * @param $clsname
 * @param int $bf
 * @return int
 */
function isRun($clsname, $bf = 0)
{
    //下面进行检测,如有一个进程正在运行,则不运行
    $str = shell_exec("ps ax > " .__DIR__ .'/'.$clsname . "_run.txt");
    $str = shell_exec("grep -c '" .$clsname . "' " . __DIR__.'/'. $clsname . "_run.txt");
  
    if ($bf > 0) {
        if ($str >= $bf) {
            return 1;
        } else {
            return 0;
        }
    } else {
        if ($str >= 2) {
            return 1;
        } else {
            return 0;
        }
    }
}
  
define('QUEUE', 'RedisQueueServer');
define('HTTP', 'httpd');
define('NG', 'nginx');
define('REDIS', 'redis-server');
define('SQL', 'mysql');
  
//检查队列进程
if (isRun(QUEUE)) {
    make_log('RedisQueueServer is running !');
} else {
    make_log('RedisQueueServer is not running ! try to start it....');
    exec('php /WanQueue/console/RedisQueueServer.php');
    if (isRun(QUEUE)) {
        make_log('RedisQueueServer start success !');
    } else {
        make_log('RedisQueueServer start failed !');
        sendEmail('12312313,12312313', '[APPNAME-ServerError]:服务器的队列进程RedisQueueServer挂了,自动重启失败!!');
    }
}
//检查sql进程
if (isRun(SQL)) {
    make_log('mysql is running !');
} else {
    make_log('mysql is not running ! try to start it....');
    exec('service mysql start');
    if (isRun(SQL)) {
        make_log('mysql start success !');
    } else {
        make_log('mysql start failed !');
        sendEmail('12312313,12312313', '[APPNAME-ServerError]:服务器的队列进程mysql挂了,自动重启失败!!');
    }
}
  
  
//检查httpd进程
if (isRun(HTTP)) {
    make_log('httpd is running !');
} else {
    make_log('httpd is not running ! try to start it....');
    exec('httpd -k start');
    if (isRun(HTTP)) {
        make_log('httpd start success !');
    } else {
        make_log('httpd start failed !');
        sendEmail('12312313,12312313', '[APPNAME-ServerError]:服务器的队列进程httpd挂了,自动重启失败!!');
    }
}
//检查nginx进程
if (isRun(NG)) {
    make_log('nginx is running !');
} else {
    make_log('nginx is not running ! try to start it....');
    exec('nginx');
    if (isRun(NG)) {
        make_log('nginx start success !');
    } else {
        make_log('nginx start failed !');
        sendEmail('12312313,12312313', '[APPNAME-ServerError]:服务器的队列进程nginx挂了,自动重启失败!!');
    }
}
//检查redis进程
if (isRun(REDIS)) {
    make_log('redis is running !');
} else {
    make_log('redis is not running ! try to start it....');
    exec('nohup redis-server &');
    if (isRun(REDIS)) {
        make_log('redis start success !');
    } else {
        make_log('redis start failed !');
        sendEmail('12312313,12312313', '[APPNAME-ServerError]:服务器的队列进程redis-server挂了,自动重启失败!');
    }
}

网友评论