加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

进程守护程序

发布时间:2020-12-15 07:14:19 所属栏目:安全 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/bin/bash#=========================================# Filename : watch.sh# Filetype : Shell# Author : Colben# Create : 2015-09-08 01:16:35#

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#!/bin/bash
#=========================================
# Filename : watch.sh
# Filetype : Shell
# Author   : Colben
# Create   : 2015-09-08 01:16:35
#=========================================
  
[ 2 -gt $# ] && echo -e "tUsage:$0 {start|stop} {cmd}n" && exit 1

pname=`basename $2`
function stop_pw
{
    ppid=`ps -ef|grep "`basename $0` +start +[^ ]*&;$pname&;"|awk '{print $2}'`
    [ '' != "$ppid" ] && ps -A -o pid,ppid|grep " $ppid$"|awk '{print $1}'|xargs kill && kill $ppid
    exit $1
}
case $1 in
    start)
        ps -ef|grep "`basename $0` +start +[^ ]*&;$pname&;"|grep -v " +$$ +"
        [ 0 -eq $? ] && echo "$pname is runing,WATCH can not start ..." && exit 2
        pid=0000
        last_stat=0
        last_time=`date +%s`
        shift
        while :
        do
            if [ ! -f /proc/$pid/stat ]
            then
                trap "stop_pw 4" 2 3 15
                $* > /dev/null 2>/dev/null &
                pid=$!
            else
                let "interval=`date +%s`-$last_time"
                [ 600 -gt $interval ] && sleep 4 && continue
                cur_stat=`cat /proc/$pid/stat|awk 'END{print $15+$16}'`
                [ $cur_stat -ne $last_stat ] && last_stat=$cur_stat || kill $pid && sleep 2
                last_time=`date +%s`
            fi
        done >/dev/null 2>/dev/null &
        ;;
    stop)
        stop_pw 0
        ;;
    *)
        echo -e "tUsage:$0 {start|stop} {cmd}n"
        ;;
esac

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读