zoukankan      html  css  js  c++  java
  • 使用cron+php脚本监控后台任务脚本

    task1.php

    <?php error_reporting(E_ERROR | E_WARNING | E_PARSE);
    // redis
    $redis = new Redis();
    $redis->pconnect(REDIS_HOST, REDIS_PORT);
    $redis->auth(REDIS_PASS);
    // task
    while (true) {
        $task = $redis->rPop('mytask1');
        if ($task !== false) {
            // todo
            continue;
        }
        sleep(1);
    }
    ?>

    task_monitor.php

    <?php error_reporting(E_ERROR | E_WARNING | E_PARSE);
    // 任务数组
    $tasks = array(
        'task1.php',
        'task2.php',
        'task3.php',
        'task4.php',
        'task5.php'
        );
    // 查找正在运行的任务
    $cmd = "ps -ef | grep 'task'";
    $res = shell_exec($cmd);
    $all = explode("
    ", $res);
    $running = array();
    for($i=0; $i<count($all); $i++){
        $array = explode("php ", $all[$i]);
        $p = trim($array[1]);
        if(!empty($p)) {
            $p = str_replace('/mytask/', '', $p);
            $running[] = $p;
        }
    }
    echo(date('Y-m-d H:i:s')."
    ");
    $date=date('YmdHis');
    // 查找不在运行的任务并启动任务
    for($i=0; $i<count($tasks); $i++){
        if(in_array($tasks[$i], $running)) {
            echo($tasks[$i]." is running
    ");
        } else {
            echo($tasks[$i]." is dead
    ");
            $cmd = "nohup /usr/bin/php /mytask/".$tasks[$i]." >/mytask/nohup/mytask.log 2>&1 &";
            $res = shell_exec($cmd);
        }
    }
    echo("
    ");
    ?>
  • 相关阅读:
    Mysql 之 编码问题
    Mysql Basic
    CentOS 之 Sublime text3 安装
    mapper映射
    端口号占用
    2020系统综合实验 第7次实践作业
    2020系统综合实验 第6次实践作业
    解决github打开速度慢问题
    2020系统综合实践 第5次实践作业
    2020系统综合实验 第4次实践作业
  • 原文地址:https://www.cnblogs.com/coffee_cn/p/12101382.html
Copyright © 2011-2022 走看看