惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - coffee

apache配置http跳转https centos7使用yum安装mysql5.7 Jboot实现Redis操作事件通知 sun.misc.BASE64Encoder找不到jar包的解决方法 使用命令修改git的用户名和提交的邮箱 apache,请求来源特定域名,重定向到图片域名。 Shell_exec php with nohup git常用命令 Jenkins+Gitee异常解决 kafka安装和简单测试 statsvn使用小记 java.net.URLEncoder对中文的编码和解码 Apache配置WebSocket代理 php批量检查https证书有效期 504 Gateway Time-out ( Nginx + PHP ) 解决小计 VMware 扩展磁盘容量 查看mysql字符集、修改数据库、数据表、字段字符集 Docker常用命令 Docker 安装 MySQL 学习笔记
使用cron+php脚本监控后台任务脚本
coffee · 2019-12-26 · via 博客园 - coffee

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");
?>

posted on 2019-12-26 12:20  coffee  阅读(791)  评论()    收藏  举报