Linux看門狗指令碼 1 4

2021-09-07 05:57:47 字數 3733 閱讀 6377

近期專案的看門狗經歷了三個版本號。

第乙個版本號:

用ps -ef,假設程式掛了就啟動

第二個版本號:

程式因為執行時會出現不再監聽7901port,所以不能簡單推斷機器是不是掛了,而是推斷此port是否有監聽

第三個版本號:

當7901port不再監聽,就先把原來的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 "[$] monitor start...."

echo "[$] monitor start...." >> $/log/$

port=7905

tcplisteningnum=`netstat -an | grep ":$port " | awk '$1 == "tcp" && $nf == "listen" ' | wc -l`

if [ $tcplisteningnum = 1 ]

then

] the $port is listening"

}else

] 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" ' | wc -l`

if [ $tcplisteningnum = 1 ]

then

] the $ is listening" >> $/log/$

} else

] the $ is not listening" >> $/log/$

echo "[$] killall scp_platform now !" >> $/log/$

kscp

echo "[$] check $ quit, now restart $ ..." >> $/log/$

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 "[$] monitor start...."

echo "[$] monitor start...." >> $/log/$

port=7905

tcplisteningnum=`netstat -an | grep ":$port " | awk '$1 == "tcp" && $nf == "listen" ' | wc -l`

if [ $tcplisteningnum = 1 ]

then

] the $port is listening"

}else

] 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" ' | wc -l`

if [ $tcplisteningnum = 1 ]

then

] the $ is listening" >> $/log/$

} else

] the $ is not listening" >> $/log/$

echo "[$] killall scp_platform now !" >> $/log/$

killall scp_platform

echo "[$] check $ quit, now restart $ ..." >> $/log/$

nohup scp_platform&

} fi

sleep 180

done

這裡之所以要sleep 180是是由於程式載入實際略微有點長,要不然載入還沒完畢的時候是不能夠推斷有沒有監聽7905port的

原來版本號的env.sh #無需改動就可以使用

env.sh主要是環境變數設定和自己定義的變數

#bin/bash

export root=/root/scp

export work_dir=$

export include=$/include

export otl=$/otl_mysql

export ld_library_path=$/lib:/usr/local/lib

export ace_root=$

export odbcini=/usr/local/etc/odbc.ini

export odbcsysini=/usr/local/etc

path=$:$/bin

export path

odbcinst -j

alias wk='cd $'

alias bin='cd $/bin'

alias cfg='cd $/conf'

alias rmlog='rm -rf $/bin/log*.*; rm -rf $/log/*.*'

alias lis='netstat -an|grep -i 7905'

alias scp='$/bin/scp_platform &'

alias moni='$/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

linux shell實現守護程序 看門狗 指令碼

嵌入式初學者,第一次上傳 昨天做了乙個udhcpd與udhcpc的守護,目前只會用shell模仿編寫,還有什麼方法可以做守護呢?1 2345 6789 1011 1213 1415 1617 1819 2021 2223 2425 2627 2829 30 bin sh 程序名字可修改 pro na...

LINUX系統看門狗

arm linux開發 warewin 2g 3g無線傳輸 dtu 和路由器 筆記 看門狗定時器 wdt watch dog timer 它實際上是乙個 計數器,一般給看門狗乙個大數,程式 開始執行後看門狗開始倒計數。如果程式 執行正常,過一段時間cpu 應發出指令讓看門狗 復位,重新開始倒計數。如...

Linux 軟體看門狗 watchdog

linux 自帶了乙個 watchdog 的實現,用於監視系統的執行,包括乙個核心 watchdog module 和乙個使用者空間的 watchdog 程式。核心 watchdog 模組通過 dev watchdog 這個字元裝置與使用者空間通訊。使用者空間程式一旦開啟 dev watchdog ...