程序挂了自动重启的守护脚本

#! /bin/bash
export PSGSMFES_HOME=/home   #可执行程序绝对路径
PROC_PATH=./     #当前目录

PROC_ARRAY[0]=proName  # 可执行程序名称,这里是数组,可以守护多个程序



PID=0
DIR_HOME=${PSGSMFES_HOME}
#LD_LIBRARY_PATH=/usr/local/PSGSMStation/lib;/lib;/lib64
LOG_DIR=/log/run_log

count=0   #重启次数初始化0
touch ${PSGSMFES_HOME}/log/psgsmaudioRestartFile  #记录重启次数的文件

while [ 1 -eq 1 ]
do
        
	for pro_name in ${PROC_ARRAY[@]};
	do
		echo Check the Progress $pro_name......
		if [ `pgrep -x $pro_name | wc -l` -eq 1 ]
		then 
			echo $pro_name is already running
		elif [ `pgrep -x $pro_name | wc -l` -lt 1 ]
		then 
			echo $pro_name will be started!!
			cd $PROC_PATH
			if [ $pro_name == "devicemanager" ]     #注意下面路径多出个/bin,保证路径对
			then
				rm ${PSGSMFES_HOME}/bin/${pro_name}.lock -f
				${PSGSMFES_HOME}/bin/$pro_name -version etcd> /dev/null 2>&1 &
			elif [ $pro_name == "ntpd" ]
			then
				rm ${PSGSMFES_HOME}/bin/${pro_name}.lock -f
				${PSGSMFES_HOME}/bin/$pro_name -g> /dev/null 2>&1 &
		        else
				((count++));
				rm ${PSGSMFES_HOME}/bin/${pro_name}.lock -f
				${PSGSMFES_HOME}/bin/$pro_name > /dev/null 2>&1 &     #重启
				echo $count > ${PSGSMFES_HOME}/log/psgsmaudioRestartFile  #重启次数写入文件
			fi
			sleep 1
			#PID=`pgrep -f $pro_name`
		else
			echo $pro_name will be killed!!
		fi
	done
	# wait for sometime, if 1 minute,  sleep 1m instead
	#rm core-*
	sleep 3  #监测时间间隔
done

可使用 setsid 命令启动脚本, 即后台运行,这样 就会一直保证程序运行着

你可能感兴趣的:(linux操作及其他)