守护进程脚本

#!/bin/sh
#nohup TransferTool_daemon.sh start &
#author zhongshusong
#mail [email protected]
#time 2018-06-21
#phone 18276602417
TransferToolpath=$(cd "$(dirname "$0")"; pwd)
process_name=TransferTool_ccid_1.jar
time1(){
t1=`date -d $(date|awk -F ' ' '{print $4}') +%s`
t2=`date -d "07:00:00" +%s`
t3=`date -d "20:00:00" +%s`
if [ $t1 -gt $t2 -a $t1 -lt $t3 ];then
        n=0
else
        n=1
fi
}

start(){
        while (true)
        do
        sleep 6
        process_num=($(ps -ef|grep $process_name|grep -v grep|wc -l))
        time1
                if [ $process_num -eq 0 -a $n -eq 0 ];then
                        date1=`date +%F`
                        cd $TransferToolpath
                        nohup java -jar $process_name -startup $date1 07:00:00 $date1 22:00:00 &
                        if [ $? -eq 0 ];then

                                echo "the process starting ok"
                        else
                                echo "the process starting fasle"
                        fi

                elif [ $process_num -gt 0 -a $n -eq 1 ];then
                        kill -9 `ps -ef |grep $process_name|grep -v "grep"|awk '{print $2}'`

                else
                        
                        echo "the process is  running " >>/dev/null
                fi
        done
}

stop(){
        process_num=($(ps -ef|grep $process_name|grep -v grep|wc -l))
        process_name1=$0
        if [ $process_num -eq 0 ];then
                echo " " >>/dev/null
        else
                kill -9 `ps -ef |grep $process_name|grep -v "grep"|awk '{print $2}'`
                kill -9 `ps -ef |grep $process_name1|grep -v "grep"|awk '{print $2}'`
        fi

}

status(){
        process_num=($(ps -ef|grep $process_name|grep -v grep|wc -l))
        if [ $process_num -eq 0 ];then
                echo " the process is not running" >>/dev/null
        else
                ps -ef |grep $process_name|grep -v "grep"
        fi
}

case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
*)
echo $"Usage: $0 {start|stop|status}"
;;
esac
exit $?

猜你喜欢

转载自blog.csdn.net/weixin_42386617/article/details/87690268