<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    so true

    心懷未來,開創未來!
    隨筆 - 160, 文章 - 0, 評論 - 40, 引用 - 0
    數據加載中……

    watchdog_pid.sh

    #!/bin/bash
    #ulimit -c unlimited
    export PS4='+{$LINENO($(date +"%F %T")):${FUNCNAME[0]}} '
    full_path=$(readlink -f $0)
    #cd $(dirname $full_path)
    #exec 1>${full_path%.*}.log
    exec 2>${full_path%.*}.err
    set -x
    usage() {
        printf \
    "Usage: %s <pid|pid_file> <start_cmd|start_script>\n\n \
     pid|pid_file                 process pid or the pid file containing the pid of process to be supervised\n \
     start_cmd|start_script       start commands or start script if process dead\n \
    \n" $1
        exit 1
    }
    if [ $# -lt 2 ]; then
        usage $0
    fi
    pid_way=$1
    if [ "" != "${pid_way//[0-9]}" -a -s "$pid_way" ]; then
        pid_file=$(readlink -f $pid_way)
    else
        pid_id=$pid_way
        pinfo=($(ps -eo uid,pid,args | awk -v pid=$pid_id '{if($2==pid)print $0}'))
        puid=${pinfo[0]}
        pname=${pinfo[2]}
        if [ "" == "$puid" -o "" == "$pname" ]; then
            echo "Couldn't find the process with the pid $pid_id"
            exit 2
        fi
    fi
    cmd=($2)
    if [ -s "${cmd[0]}" ]; then
        start_way=$(readlink -f ${cmd[0]})
        for ((i=1; i<${#cmd[@]}; ++i)); do
            start_way="$start_way ""${cmd[$i]}"
        done
        shift 2
        start_way="$start_way ""$@"
    else
        shift 1
        start_way="$@"
    fi
    while true; do
        if [ "" != "$pid_id" ]; then
            pid=$pid_id
        else
            pid=$(cat $pid_file)
        fi
        if [ "" != "$pid" ]; then
            if ! kill -0 $pid; then
                now=$(date +%s)
                $start_way            
                #sleep 60
                if [ "" != "$pid_id" ]; then
                    tm_fields_num=$(ps -eo lstart | tail -1 | awk '{print NF}')
                    find_new_process=0
                    pid_id_bak=()
                    while read line; do
                        pstarttime=$(date -d "${line#* }" +%s) 
                        if [ "$pstarttime" -ge "$now" ]; then
                            pid_id_tmp=${line%% *}
                            pinfo_new=($(ps -eo uid,pid,args | awk -v pid=$pid_id_tmp '{if($2==pid)print $0}'))
                            if [ "0" == "${#pinfo_new[@]}" ]; then
                                continue
                            fi
                            diff_fields_num=0
                            for ((i=3; i<(${#pinfo[@]} > ${#pinfo_new[@]} ? ${#pinfo[@]} : ${#pinfo_new[@]}); ++i)); do
                                if [ "${pinfo[$i]}" != "${pinfo_new[$i]}" ]; then
                                    ((++diff_fields_num))
                                fi
                            done
                            if [ "0" -eq "$diff_fields_num" ]; then
                                pid_id=$pid_id_tmp
                                if [ "$((++find_new_process))" -gt "1" ]; then
                                    exit 1
                                fi
                            else
                                pid_id_bak[$diff_fields_num]=$pid_id_tmp
                            fi
                        fi
                    done < <(ps -eo uid,pid,lstart,args | awk -v puid=$puid -v pname=$pname -v tfn=$tm_fields_num '{if($1==puid && $(3+tfn)==pname){printf "%d ", $2; for(i=3;i<3+tfn;++i) printf "%s ", $i; printf "\n";}}')
                    if [ "0" == "$find_new_process" ]; then
                        pid_id=${pid_id_bak[@]}
                        pid_id=${pid_id%% *}
                    fi
                else
                    tm_new_pid_file=$(date -d "$(stat $pid_file | grep ^Modify: | sed 's#Modify:##')" +%s)
                    if [ "$tm_new_pid_file" -lt "$now" ]; then
                        exit 2
                    fi
                fi
            fi
        fi
        #sleep 1
        if [ "$(ls -l ${full_path%.*}.err | awk '{print $5}')" -ge $((1024 * 1024 * 1024)) ]; then
            set +x
            exec 2>/dev/null
            cp ${full_path%.*}.err ${full_path%.*}.err.last
            rm -f ${full_path%.*}.err
            exec 2>${full_path%.*}.err
            set -x
        fi
    done

    posted on 2017-04-06 16:00 so true 閱讀(231) 評論(0)  編輯  收藏 所屬分類: Linux

    主站蜘蛛池模板: 无码人妻久久一区二区三区免费丨| 本道天堂成在人线av无码免费| 久久午夜免费鲁丝片| 亚洲色精品aⅴ一区区三区| 九月丁香婷婷亚洲综合色| 久久久久久亚洲精品| 免费看男人j放进女人j免费看| 国产亚洲精久久久久久无码AV| 一级做受视频免费是看美女 | 国产成人亚洲综合在线| 国产成人不卡亚洲精品91| 日本黄页网站免费| 美女黄频视频大全免费的| 亚洲女同成人AⅤ人片在线观看| 久久精品国产亚洲av麻豆小说| 无码国产精品一区二区免费式芒果 | 亚洲国产精彩中文乱码AV| 久久精品免费观看国产| 亚洲精品国产情侣av在线| 一二三四影视在线看片免费| 亚洲美国产亚洲AV| 9420免费高清在线视频| 亚洲国产高清精品线久久| 中国一级特黄高清免费的大片中国一级黄色片 | 亚洲一卡一卡二新区无人区| 91精品手机国产免费| 亚洲18在线天美| 久久午夜夜伦鲁鲁片免费无码 | 亚洲AV无码AV日韩AV网站| 亚洲综合免费视频| 亚洲αv在线精品糸列| 人妖系列免费网站观看| 久久久久亚洲精品美女| 成人性生交大片免费看午夜a| 四虎精品免费永久免费视频| 亚洲高清在线视频| 成人超污免费网站在线看| 精品乱子伦一区二区三区高清免费播放| 亚洲成av人片在线观看无码不卡| 免费无码黄十八禁网站在线观看 | 亚洲精品午夜国产VA久久成人|