1. 程式人生 > >用shell寫守護程序指令碼

用shell寫守護程序指令碼

 一個udhcpd與udhcpc的守護,目前只會用shell模仿編寫,還有什麼方法可以做守護呢?

#! /bin/sh
#程序名字可修改
PRO_NAME=udhcpc
WLAN=ra0
  
while true ; do
  
#    用ps獲取$PRO_NAME程序數量
  NUM=`ps aux | grep ${PRO_NAME} | grep -v grep |wc -l`
#  echo $NUM
#    少於1,重啟程序
  if [ "${NUM}" -lt "1" ];then
    echo "${PRO_NAME} was killed"
    ${PRO_NAME} -i ${WLAN}
#    大於1,殺掉所有程序,重啟
  elif [ "${NUM}" -gt "1" ];then
    echo "more than 1 ${PRO_NAME},killall ${PRO_NAME}"
    killall -9 $PRO_NAME
    ${PRO_NAME} -i ${WLAN}
  fi
#    kill殭屍程序
  NUM_STAT=`ps aux | grep ${PRO_NAME} | grep T | grep -v grep | wc -l`
  
  if [ "${NUM_STAT}" -gt "0" ];then
    killall -9 ${PRO_NAME}
    ${PRO_NAME} -i ${WLAN}
  fi
done
  
exit 0

 寫一個flume的守護程序shell指令碼

#! /bin/sh
#程序名字可修改
PRO_NAME=flume

while true ; do

#    用ps獲取$PRO_NAME程序數量
  NUM=`ps aux | grep ${PRO_NAME} | grep -v grep |wc -l`
#  echo $NUM
#    少於1,重啟程序
  if [ "${NUM}" -lt "1" ];then
    echo "flume程序異常,正在重啟"
    /flume_app/apache-flume-1.6.0-bin/bin/flume-ng agent -n a1 -c conf -f /flume_app/apache-flume-1.6.0-bin/conf/b.conf -Dflume.root.logger=INFO,console

  fi
  sleep 1
done
exit 0