Shell实现判断进程是否存在并重新启动脚本
详细版:
#!/bin/bash
#
#调用关闭jboss进程脚本
stopMethodServer.sh
#打印出当前的jboss进程:grep jboss查询的jboss进程,grep -v "grep" 去掉grep进程
jmsThread=`ps -ef | grep gdms | grep jboss | grep -v "grep"`
echo $jmsThread
#查询jboss进程个数:wc -l 返回行数
count=`ps -ef | grep gdms | grep jboss | grep -v "grep" | wc -l`
echo $count
sec=7
#开始一个循环,以判断进程是否关闭
for var in 1 2
do
if [ $count -gt 0 ]; then
#若进程还未关闭,则脚本sleep几秒
echo sleep $sec second the $var time, the JMS thread is still alive
sleep $sec
else
#若进程已经关闭,则跳出循环
echo "break"
break
fi
done
#if [ $count -eq 0 ]; then
# echo "nohup startMethodServer.sh &"
# nohup startMethodServer.sh &
#else
# echo "It's better to check the thread!!!"
#fi
#调用启动脚本
nohup startMethodServer.sh &
详细zabbix版本:
#!/bin/bash
:<<\EOF
About configuration file scan
Script author is neo_will
EOF
#Print out the current Zabbix process: the zabbix process of the grep JBoss query, and the grep-v "grep" removes the grep process
Port_Zabbix=` ps -ef|grep zabbix_agentd |grep -v "grep"`
IP_Own=`ip a | grep tap0 | grep inet | awk '{print $2}' | awk -F'/' '{print $1}'`
port=(`ps -ef |grep 'zabbix' | awk '{print $3}'| sed -n '3p' |awk -F':' '{if($NF~/^[0-9]*$/) print $NF}' |sort |uniq`)
echo $Port_Zabbix
sleep 1
#Query the number of zabbix processes: wc-l returns rows
Count_zabbix=`ps -ef | grep zabbix_agentd | grep -v "grep" | wc -l`
echo $Count_zabbix
sleep 1
#Defining variables
Zabbix_AgentdFile="/usr/local/zabbix/sbin/zabbix_agentd"
#Determine whether a file exists
if [ ! -f "$Zabbix_AgentdFile" ]; then
echo "$IP_Own File exists" >> /opt/zabbix_scanning.txt
else
echo "$IP_Own file does not exist" >> /opt/zabbix_scanning.txt && bash zabbix_agent3-2-0guoke666666.sh
fi
sleep 60
sec=7
#Start a loop to determine if the process is closed
for var in 1 2
do
if [ $count -gt 0 ]; then
#If the process is not closed, the script sleep for a few seconds
echo sleep $sec second the $var time, the $IP_Own zabbix thread is still alive >> /opt/zabbix_scanning.txt
sleep $sec
else
#If the process is closed, jump out of the loop
echo "If the $IP_Own process is closed, jump out of the loop and break " >> /opt/zabbix_scanning.txt
service zabbix_agentd restart && break
fi
done
还没有评论,来说两句吧...