• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

使用cron+php脚本监控后台任务脚本

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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("\n", $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')."\n");
$date=date('YmdHis');
// 查找不在运行的任务并启动任务
for($i=0; $i<count($tasks); $i++){
    if(in_array($tasks[$i], $running)) {
        echo($tasks[$i]." is running\n");
    } else {
        echo($tasks[$i]." is dead\n");
        $cmd = "nohup /usr/bin/php /mytask/".$tasks[$i]." >/mytask/nohup/mytask.log 2>&1 &";
        $res = shell_exec($cmd);
    }
}
echo("\n");
?>

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHPFrameworkMVCBenchmark基准测试发布时间:2022-07-10
下一篇:
PHP结合AJAX实现搜索提示功能发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap