Shell 脚本监控java进程

# ps -ef|grep tomcat_community|grep -v 'grep'|awk '{print $2}'

 # 这里的-f参数判断$myFile是否存在 
pidfile=java.pid
if [ ! -f "$pidfile" ]; then 
        touch ${pidfile};
        echo "$pidfile new create finish";
fi

# $! 后台运行的最后一个进程的进程ID号
trap "" 1 2 3 24

while true
do
        if [ -z $(cat $pidfile) ] ; then
                echo "$pidfile is empty";
        fi;

        pid=$(ps -ef|grep java|grep lf-lfzb.jar|grep $(cat $pidfile)|awk '{print $2}')

        echo "current mode lf-lfzb.jar java pid value is " $pid;

        if [ "$pid" != $(cat $pidfile) ] ; then 
        #./shutdown.sh
        /opt/jdk/bin/java -jar lf-lfzb.jar  -Dfile.encoding=UTF-8 >>/dev/null  & echo $! > ${pidfile}
        fi
        sleep 5
#break
done

 

猜你喜欢

转载自jadeluo.iteye.com/blog/2248541