欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux:看门狗watchdog.sh程序编写示例

程序员文章站 2022-07-12 10:19:50
...

学习和借鉴前人经验,加之美化一番 

###
 # @Author: hanxiaoshun
 # @Date: 2020-07-08 15:53:34
 # @LastEditTime: 2020-09-05 10:54:45
 # @LastEditors: Please set LastEditors
 # @Description: In User Settings Edit
 # @FilePath: \shunzi\config\owatchdog.sh
### 
#!/bin/sh
#kill -9 $(ps -ef|grep owatchdog.sh | grep -v grep | awk '{print $2}')
# 监控node 服务
chktask_node(){
  #echo "check task name with" $1
  pidcount=`ps -ef|grep $1 | grep -v grep |wc -l`
  if [ $pidcount -eq 0 ]; then
      #echo "no run"
      node /home/shunzi/node/testServer.js > /dev/null 2>&1
      echo "$CURTIME run $1 " >> /tmp/owatchdog_log.txt
  fi
}
# 监控python server服务
chktask_python3(){
  echo "check task name with" $1
  pidcount=`ps -ef|grep $1 | grep -v grep | wc -l`
  if [ $pidcount -eq 0 ]; then
      python3 /home/shunzi/python/test_flask_server.py >/dev/null 2>&1 &
      echo "$CURTIME run $1 " >> /tmp/owatchdog_log.txt
  fi
}

while true
do
    CURTIME=`date "+%Y-%m-%d %H:%M:%S"`
    
    # 监控服务
    if [ -d "/home/shunzi/python/" ]; then
        cd/home/shunzi/python/
        chktask_python3 test_flask_server.py
    fi
    if [ -d "/home/shunzi/node/" ]; then
        cd /home/shunzi/node/
        chktask_node testServer.js
    fi
    
    # 监控脚本
    #PID=`ps -ef|grep watch_test | grep -v grep | wc -l`
    #if [ $PID -eq 0 ]; then 
    #  if [ -f /usr/bin/watch_test.sh ]; then
    #      /usr/bin/watch_test.sh &
    #  fi
    #fi
    
    # 监控命令行
    count=`ps -ef|grep redis|grep -v grep|wc -l`
    if [ $count -eq 0 ]; then
      if [ -f /home/shunzi/redis_place/redis/local/redis/bin/redis-server ]; then
          /home/vcontroller/redis_place/redis/local/redis/bin/redis-server /home/shunzi/redis_place/redis/local/redis/bin/redis.conf --daemonize yes --port 9673
      fi
    fi
 sleep 3

done