Linux看门狗脚本 1.4

最近项目的看门狗经历了三个版本。

第一个版本:

用ps -ef,如果程序挂了就启动


第二个版本:

程序由于运行时会出现不再监听7901端口,所以不能简单判断机器是不是挂了,而是判断此端口是否有监听


第三个版本:

当7901端口不再监听,就先把原来的killall再启动,每次输出到文件的内容都加日期,要不然根本不知道这事情啥时候发生的


第四个版本:

使用nohup让程序和监控程序的echo输出到非标准设备而是文件,这样彻底脱离shell,从而退出一个shell的时候真正实现后台运行


老版本如下:

#!/bin/sh
set +x

source env.sh

PRMGRAM=scp_platform
FILE_NAME=scp_monitor.log

Current_Time=`date +"%Y-%m-%d %H:%M:%S.%N"`
echo "[${Current_Time}] monitor start...." 
echo "[${Current_Time}] monitor start...." >> ${WORK_DIR}/log/${FILE_NAME}

port=7905

TCPListeningnum=`netstat -an | grep ":$port " | awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l`

if [ $TCPListeningnum = 1 ]
then
{
    echo "[${Current_Time}] The $port is listening"
}
else
{
    echo "[${Current_Time}] The port is not listening"
}
fi



while [ 1 ]
do
  Current_Time=`date +"%Y-%m-%d %H:%M:%S.%N"`
	TCPListeningnum=`netstat -an | grep ":$port " |   awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l`
	if [ $TCPListeningnum = 1 ]
	then
	{
    		echo "[${Current_Time}] The ${port} is listening" >> ${WORK_DIR}/log/${FILE_NAME}
	}
	else
	{
    		echo "[${Current_Time}] The  ${port} is not listening" >> ${WORK_DIR}/log/${FILE_NAME}
	    	echo "[${Current_Time}] killall  scp_platform now !" >> ${WORK_DIR}/log/${FILE_NAME}
	    	kscp
      	echo "[${Current_Time}] check ${PRMGRAM} quit, now restart ${PRMGRAM} ..." >> ${WORK_DIR}/log/${FILE_NAME}
      	scp_platform&
	}
	fi
    sleep 180
done

新版本如下:

start_monitor.sh #此脚本负责将monitor后台运行

#!/bin/bash

#start monitor background  without console!!

nohup ./monitor.sh &

monitor.sh #实际的monitor监控程序

#!/bin/bash
set -x

nohup  ./env.sh &

PRMGRAM=scp_platform
FILE_NAME=scp_monitor.log

Current_Time=`date +"%Y-%m-%d %H:%M:%S.%N"`
echo "[${Current_Time}] monitor start...." 
echo "[${Current_Time}] monitor start...." >> ${WORK_DIR}/log/${FILE_NAME}

port=7905

TCPListeningnum=`netstat -an | grep ":$port " | awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l`

if [ $TCPListeningnum = 1 ]
then
{
    echo "[${Current_Time}] The $port is listening"
}
else
{
    echo "[${Current_Time}] The port is not listening"
}
fi



while [ 1 ]
do
  Current_Time=`date +"%Y-%m-%d %H:%M:%S.%N"`
	TCPListeningnum=`netstat -an | grep ":$port " |   awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l`
	if [ $TCPListeningnum = 1 ]
	then
	{
    		echo "[${Current_Time}] The ${port} is listening" >> ${WORK_DIR}/log/${FILE_NAME}
	}
	else
	{
    		echo "[${Current_Time}] The  ${port} is not listening" >> ${WORK_DIR}/log/${FILE_NAME}
	    	echo "[${Current_Time}] killall  scp_platform now !" >> ${WORK_DIR}/log/${FILE_NAME}
	    	killall scp_platform
      	echo "[${Current_Time}] check ${PRMGRAM} quit, now restart ${PRMGRAM} ..." >> ${WORK_DIR}/log/${FILE_NAME}
      	nohup scp_platform&
	}
	fi
    sleep 180
done

这里之所以要sleep 180是是因为程序加载实际稍微有点长,要不然加载还没完成的时候是不可以判断有没有监听7905端口的


原来版本的env.sh #无需修改即可使用
env.sh主要是设置环境变量和自定义的变量

#bin/bash
export ROOT=/root/scp
export WORK_DIR=${ROOT}
export INCLUDE=${ROOT}/include
export OTL=${INCLUDE}/otl_mysql
export LD_LIBRARY_PATH=${ROOT}/lib:/usr/local/lib
export ACE_ROOT=${INCLUDE}
export ODBCINI=/usr/local/etc/odbc.ini
export ODBCSYSINI=/usr/local/etc
PATH=${PATH}:${ROOT}/bin
export PATH
odbcinst -j


alias wk='cd ${ROOT}'
alias bin='cd ${ROOT}/bin'
alias cfg='cd ${ROOT}/conf'
alias rmlog='rm -rf ${ROOT}/bin/log*.*; rm -rf ${ROOT}/log/*.*'
alias lis='netstat -an|grep -i 7905'
alias scp='${ROOT}/bin/scp_platform &'
alias moni='${ROOT}/bin/monitor.sh &'
alias myps='ps -fu root|grep -v grep|grep -i scp'
alias mymoni='ps -fu root|grep -v grep|grep -i moni'
alias kscp='killall -9 scp_platform'
alias kmoni='killall -9 monitor.sh'
isql
alias mynet='netstat -an | grep 7905'


ulimit -c unlimited
ulimit -n 65530





  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

C++程序员Carea

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

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

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

打赏作者

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

抵扣说明:

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

余额充值