shell脚本判断进程是否存在并重启
时间:2014-09-30 20:32 来源:linux.it.net.cn 作者:it
分享一例shell脚本,用于判断进程是否存在,并实现进程重启功能,shell脚本监测linux服务进程的小例子。
shell判断进程是否存在并重新启动的脚本
1,简洁版 shell脚本:
复制代码代码示例:
#! /bin/bash
# author caoxin
# time 2012-10-10
# program : 判断进行是否存在,并重新启动
function check(){
count=`ps -ef |grep $1 |grep -v "grep" |wc -l`
#echo $count
if [ 0 == $count ];then
nohup python /runscript/working/$1 &
fi
}
check behaviors.py
2,完整版 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 &
(责任编辑:IT)
分享一例shell脚本,用于判断进程是否存在,并实现进程重启功能,shell脚本监测linux服务进程的小例子。 shell判断进程是否存在并重新启动的脚本
1,简洁版 shell脚本:
复制代码代码示例:
#! /bin/bash
# author caoxin # time 2012-10-10 # program : 判断进行是否存在,并重新启动 function check(){ count=`ps -ef |grep $1 |grep -v "grep" |wc -l` #echo $count if [ 0 == $count ];then nohup python /runscript/working/$1 & fi } check behaviors.py
2,完整版 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 & (责任编辑:IT) |