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

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

###
 # @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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千码君2016

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值