码迷,mamicode.com
首页 > 系统相关 > 详细

检查进程启动情况,开始时间、启动时间、启动进程数、进程数是否正确、PID

时间:2019-08-26 09:34:14      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:aaaaaa   eee   cccccc   pid   erro   inf   str   kkk   awk   

 

 

#!/bin/sh
bin=$(cd `dirname $0`;pwd)
cd ${bin}


### 定义检查函数
chk(){
    programName=$1
    correctNum=$2
    programSubName=$3

    # 判断是否带子程序名
    if [ -n "${programSubName}" ]
    then
        programNum=$(ps -ef|grep -Ewv "grep|less|more|zgrep|zless|vi|vim|gzip"|grep ${programName}|grep -w ${programSubName}|wc -l)
        info=($(ps -eo pid,lstart,etime,cmd |grep -Ewv "grep|less|more|zgrep|zless|vi|vim|gzip"|grep ${programName}|grep -w ${programSubName}|awk {print $1" "$3" "$4" "$5" "$6" "$7}|tail -1))
        program="${programName}|${programSubName}"
    else
        programNum=$(ps -ef|grep -Ewv "grep|less|more|zgrep|zless|vi|vim|gzip"|grep ${programName}|wc -l)
        info=($(ps -eo pid,lstart,etime,cmd |grep -Ewv "grep|less|more|zgrep|zless|vi|vim|gzip"|grep ${programName}|awk {print $1" "$3" "$4" "$5" "$6" "$7}|tail -1))
        program=${programName}
    fi

    # 解析进程信息
    pid=${info[0]}
    startTime=$(date -d "${info[1]} ${info[2]} ${info[3]} ${info[4]}" "+%Y-%m-%d %H:%M:%S")
    elapsedTime=${info[5]}

    # 判断进程数是否一致
    if [ ${programNum} -eq ${correctNum} ]
    then
        printf "${format_str}" "${program}" "ok" "${startTime}" "${elapsedTime}" "${programNum}" "${correctNum}" "${pid}"
    else
        printf "${format_str}" "${program}" "ERROR" "${startTime}" "${elapsedTime}" "${programNum}" "${correctNum}" "${pid}"
    fi
}

### 打印表头
format_str="%-33s %7s %20s %14s %12s %12s %10s\n"
printf "\n${format_str}" "ProgramName" "Status" "StartTime" "RunningTime" "ProgramNum" "CorrectNum" "PID"

### 检查Client进程
for i in `cat canalInstance`
do
    chk "CanalClient" 1 ${i}
done

### 检查Server进程
chk "AAAAAAAAAAAAAAAAAAA" 1 aaaaaaaa_00
chk "BBBBBBBBBBBBBBBBBBB" 1 bbbbbbbb_01

echo


ProgramName                        Status            StartTime    RunningTime   ProgramNum   CorrectNum        PID
CanalClient|eeeeeeeeeeeeee             ok  2019-08-20 18:14:52     5-14:21:12            1            1      19138
CanalClient|ddddddd                    ok  2019-08-20 18:14:46     5-14:21:18            1            1      18996
CanalClient|cccccc                     ok  2019-08-20 18:14:38     5-14:21:26            1            1      18723
CanalClient|ffffffff                   ok  2019-08-20 18:14:30     5-14:21:34            1            1      18654
CanalClient|hhhhhhhh                   ok  2019-08-20 18:14:24     5-14:21:40            1            1      18574
CanalClient|iiiiiiii                   ok  2019-08-20 18:14:16     5-14:21:48            1            1      18485
CanalClient|jjjjjjj                    ok  2019-08-20 18:14:11     5-14:21:53            1            1      18443
CanalClient|kkkkkkk                    ok  2019-08-20 18:14:06     5-14:21:58            1            1      18384
CanalClient|lllllll                    ok  2019-08-20 18:13:41     5-14:22:24            1            1      17437
CanalClient|mmmmmmm                    ok  2019-08-20 18:10:33     5-14:25:32            1            1      14997
CanalClient|lllllll                    ok  2019-08-20 18:10:13     5-14:25:52            1            1      14627
CanalClient|ooooooo                    ok  2019-08-20 18:09:48     5-14:26:17            1            1      13382
CanalClient|aaaaa                      ok  2019-08-20 18:21:42     5-14:14:23            1            1      28419
CanalClient|bbbb                       ok  2019-08-20 18:07:21     5-14:28:44            1            1       9664
AAAAAAAAAAAAAAAAAAA|aaaaaaaa_00        ok  2019-08-25 14:20:04       18:16:01            1            1      24445
BBBBBBBBBBBBBBBBBBB|bbbbbbbb_01        ok  2019-08-25 07:40:04     1-00:56:01            1            1       4999

 

检查进程启动情况,开始时间、启动时间、启动进程数、进程数是否正确、PID

标签:aaaaaa   eee   cccccc   pid   erro   inf   str   kkk   awk   

原文地址:https://www.cnblogs.com/chenzechao/p/11410366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!