服务启动脚本(给脚本添加启动脚本)

编写脚本

#!/bin/sh

echo $$ >/var/log/network.pid 
n=0
while true;do
  tcpdumpid=`ps aux |grep tcpdump|awk '/^tcpdump/{print $2}'`
  curl www.baidu.com &> /dev/null
  if [ $? -ne 0 ];then
    echo "$n network web curl false..." >>/var/log/error.log
    num=`ps aux | grep tcpdump | wc -l`
      if [ $num -eq 1 ];then
        tcpdump -nn -vv -i eth0 -w /var/log/server_11.cap &
      fi
  else
    kill $tcpdumpid &> /dev/null
    echo "$n network web curl ok..." >> /var/log/net.log
  fi
let n=$n+1
sleep 2
done

为其服务脚本添加启动脚本

#!/bin/sh

. /etc/init.d/functions

function start() {
  if [ -f /tmp/network.pid ];then
    action "network.sh is already running..." /bin/false
    exit 1
  else
    sh /tmp/network.sh &
    action "network.sh starting..." /bin/true
  fi
}
function stop() {
  if [ -f /tmp/network.pid ];then
    kill `cat /tmp/network.pid`
    rm -fr /var/log/network.pid
    action "network.sh stopping..." /bin/true
  else
    action "network.sh is already stopping..." /bin/false
    exit 2
  fi

}

case $1 in
  start)
    start

    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usg:$0 start|stop|restart!"
    ;;
esac

 

posted @ 2018-04-28 17:42  Me-lihu  阅读(3)  评论(0编辑  收藏  举报