The watchdog of recent projects has undergone three version numbers.
First version number:
With PS-EF, if the program hangs, it starts.
Second version number:
The program will not listen to the 7901port when it executes, so it is not easy to infer whether the machine is dead, but to infer if the port is listening
Third version number:
When the 7901port is no longer listening, the original killall will be restarted first. Every time you output to a file, add a date, or you don't even know when this is going to happen.
Fourth version number:
Use Nohup to let the program and monitor echo output to non-standard devices instead of files. So completely out of the shell, and then exit a shell to actually implement the background
The old version number is for example the following:
#!/bin/shset +xsource env.shprmgram=scp_platformfile_name=scp_monitor.logcurrent_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=7905tcplisteningnum= ' Netstat-an | grep ": $port" | awk ' $ = = ' TCP ' && $NF = = ' LISTEN ' {print $} ' | Wc-l ' if [$TCPListeningnum = 1]then{echo "[${current_time}] The $port is listening"}else{echo "[${current_time}] The port is not listening "}fiwhile [1]do current_time= ' date +"%y-%m-%d%h:%m:%s.%n "' tcplisteningnum= ' Netstat-an | grep ": $port" | awk ' $ = = ' TCP ' && $NF = = ' LISTEN ' {print $} ' | Wc-l ' if [$TCPListeningnum = 1]then{echo "[${current_time}] The ${port} is listening" >> ${work_dir}/log/${fil e_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 180done
The new version number is as follows:
Start_monitor.sh #此脚本负责将monitor后台执行
#!/bin/bash#start Monitor background without console!! Nohup./monitor.sh &
Monitor.sh #实际的monitor监控程序
#!/bin/bashset-xnohup./env.sh &prmgram=scp_platformfile_name=scp_monitor.logcurrent_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=7905tcplisteningnum= ' Netstat-an | grep ": $port" | awk ' $ = = ' TCP ' && $NF = = ' LISTEN ' {print $} ' | Wc-l ' if [$TCPListeningnum = 1]then{echo "[${current_time}] The $port is listening"}else{echo "[${current_time}] The port is not listening "}fiwhile [1]do current_time= ' date +"%y-%m-%d%h:%m:%s.%n "' tcplisteningnum= ' Netstat-an | grep ": $port" | awk ' $ = = ' TCP ' && $NF = = ' LISTEN ' {print $} ' | Wc-l ' if [$TCPListeningnum = 1]then{echo "[${current_time}] The ${port} is listening" >> ${work_dir}/log/${fil e_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 180done
Here the reason to sleep 180 is because the program load actually slightly longer, otherwise loading is not finished when it is not able to infer that there is no monitoring 7905port
The original version number of the env.sh #无需改动就可以使用
Env.sh are primarily environment variable settings and their own defined variables
#bin/bashexport root=/root/scpexport work_dir=${root}export include=${root}/includeexport OTL=${INCLUDE}/otl_ Mysqlexport Ld_library_path=${root}/lib:/usr/local/libexport Ace_root=${include}export ODBCINI=/usr/local/etc/ Odbc.iniexport odbcsysini=/usr/local/etcpath=${path}:${root}/binexport pathodbcinst-jalias 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 Mon I ' Alias kscp= ' killall-9 scp_platform ' Alias kmoni= ' killall-9 monitor.sh ' isqlalias mynet= ' Netstat-an | grep 7905 ' ulimit-c unlimitedulimit-n 65530
Linux watchdog script 1.4