侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

shell 动态进程守护

2023-12-17 星期日 / 0 评论 / 0 点赞 / 122 阅读 / 7171 字

config general general option enable 1config monitor option name 'scplc' option enable 1

config general general    option enable 1config monitor    option name 'scplc'    option enable 1    option pschkstr 'sc-serv plc'    option type     'application'    option tmpfile '/tmp/monitor/plc.mon'    option mqttkey '/app/mon/plc/status'    option timeLimit 60     option execcmd ''config monitor    option name 'scgpio'    option enable 1    option pschkstr 'sc-serv gpio'    option type     'application'    option tmpfile '/tmp/monitor/gpio.mon'    option mqttkey '/app/mon/gpio/status'    option timeLimit 60     option execcmd ''
flash="0"true="1"false="0";disable="0"enable="1"configName="scmonitor"printHelp(){	clear	echo "USAGE:"	echo "-g <monitor type> get current moitor enable status "	echo "-l view current all montoir list"	echo "-s <monitorName> <monitorValue> set monitor state"	echo "-a autoManage "	echo "-e enable cron"	echo "-d disable cron"	echo "add <name> <enable> <pschkstr> <sevicename> <tmpfile> <mqttkey> <execcmd> <timeLimit>"  echo "example:"	echo "	check -l"	echo "			plc"	echo "			pjlink"	echo "   check -g plc"	echo "			current monitor state:0"	echo "   check -s plc 0"}#检测线程是否存在function_thread_check(){	#count= ${ps |grep 'sc-serv plc' | grep -v 'grep' | wc -l}	count=$( ps | grep "$*" | grep -v 'grep' | wc -l )	if [ 0 == $count ];then		echo false	else		echo true	fi}#获取当前所有模块的名称function_getMonitorList(){	count=$(uci get $configName.general.count)	for i in $(seq 0 $count);	do		name=$(uci get $configName.@monitor[$i].name)		echo $name	done}#获取状态function_getMonitorStats(){    count=$(uci get $configName.general.count)    for i in $(seq 0 $count);    do		name=$(uci get $configName.@monitor[$i].name)        if [ "$name" == "$1" ]; then            state=$(uci get $configName.@monitor[$i].enable)            echo $1 stats is $state        fi    done}#获取当前的时间差function_getTimeCompare(){	currTime=$( date +%s)	lastTime=$1	#tmpTime= $("$currTime - $lastTime")	echo $(($currTime - $lastTime))}function_setMonitorState(){	count=$(uci get $configName.general.count)	for i in $(seq 0 $count);	do		name=$(uci get $configName.@monitor[$i].name)        old_sate=$(uci get $configName.@monitor[$i].enable)		if [ "$name" == "$1" ]; then			state=$(uci set $configName.@monitor[$i].enable=$2)            echo "$1 set success"		fi	done}# 自动化处理流程function_autoManage(){	count=$(uci get $configName.general.count)	monitorState=$(uci get $configName.general.enable)	if [ $monitorState == "0" ]; then		echo "current server monitor disable"		return 	fi	for i in $(seq 0 $count);	do		name=$(uci get $configName.@monitor[$i].name)        psChkstr=$(uci get $configName.@monitor[$i].pschkstr)		tmpfile=$(uci get $configName.@monitor[$i].tmpfile)        type=$(uci get $configName.@monitor[$i].type)		result=$(function_thread_check $psChkstr)		MonitorStats=$(uci get $configName.@monitor[$i].enable)		if [ "$MonitorStats" == "$disable" ]; then 			echo  $name current Current status is disable 			continue			fi		if [ "$result" == "false" ]; then      echo "Services have been shut down, which is restarting"			#execCmd=$(uci get sc-monitor.@monitor[$i].execcmd)			$(/etc/init.d/$name start)		else      if [ $type == "application" ]; then #application不做文件检测          echo "$name Operating normally"          continue      fi			if [ ! -e "$tmpfile" ]; then				$(/etc/init.d/$name stop)				$(/etc/init.d/$name start)				echo "restart $name server"                continue			else				lastTime=$(cat $tmpfile)				timeLimit=$(uci get $configName.@monitor[$i].timeLimit)				result=$(function_getTimeCompare $lastTime )				if [ "$result" -gt "$timeLimit" ]; then  #如果当前返回的值大于允许值					$(rm -rf $tmpfile)					$(/etc/init.d/$name stop)					$(/etc/init.d/$name start)				fi				echo $result $timeLimit			fi		fi        echo "$name Operating normally"	done}#移除服务function_removeMonitor(){	MonitorName=$1	tmp=$(uci get $configName.general.count)	for i in $(seq 0 $tmp);	do		name=$(uci get $configName.@monitor[$i].name)		if [ "$name" == "$MonitorName" ]; then			count=$((  $tmp - 1 ))			$(uci delete $configName.@monitor[$i])			$(uci set $configName.general.count=$count)			$(uci commit $configName)		fi	done}function_EnableCron(){	$(uci set $configName.general.enable=1)	$(uci commit $configName)}function_DisableCron(){	$(uci set $configName.general.enable=1)	$(uci commit $configName)}[ "$(cat /proc/uptime | awk -F. '{print $1}')" -gt 300 ] || exit 0if [ "$#" -eq 0 ]; then	printHelp	exit 1fiif [ "$1" == "-s" ]; then	if [  "$2" == "" ] || [ "$3" == "" ]; then		exit 1	fi	function_setMonitorState $2 $3	exit 1fiif [ "$1" == "add" ]; then	if [ "$2" == "" ]; then		echo "plese input type"		exit 1	fi	if [ "$3" == "" ]; then		echo "plese input eanbled"		exit 1	fi	if [ "$4" == "" ]; then		echo "plese input pschkstr"		exit 1	fi	if [ "$5" == "" ]; then		echo "plese input sevicename "		exit 1	fi	if [ "$6" == "" ]; then		echo "plese input tmpfile"		exit 1	fi	if [ "$7" == "" ]; then		echo "plese input mqttkey"		exit 1	fi	if [ "$8" == "" ]; then		echo "plese input execcmd"		exit 1	fi	if [ "$9" == "" ]; then		echo "plese input timeLimit"		exit 1	fi	tmp=$(uci get $configName.general.count)	#$count=$(( $count + 1 ))	count=$((  $tmp + 1 ))	$(uci add $configName monitor)	$(uci add_list $configName.@monitor[$count].name="$2")	$(uci add_list $configName.@monitor[$count].enable="$3")	$(uci add_list $configName.@monitor[$count].pschkstr="$4")	$(uci add_list $configName.@monitor[$count].servicename="$5")	$(uci add_list $configName.@monitor[$count].tmpfile="$6")	$(uci add_list $configName.@monitor[$count].mqttkey="$7")	$(uci add_list $configName.@monitor[$count].execcmd="$8")	$(uci add_list $configName.@monitor[$count].timeLimit="$9")	$(uci set $configName.general.count=$count)	$(uci commit $configName)	exit 1fiwhile getopts g:s:lear:d optdo	case "$opt" in		g)		function_getMonitorStats $OPTARG		;;		e)		function_EnableCron		;;		d)	function_DisableCron		;;		l)		function_getMonitorList		;;		r)		function_removeMonitor $OPTARG		;;		a)		function_autoManage		;;		?)		clear		printHelp ;;	esacdone

广告 广告

评论区