码迷,mamicode.com
首页 > 编程语言 > 详细

运维经验分享(五)-- 改进的java进程管理的服务控制脚本

时间:2015-07-23 00:59:39      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:服务管理   java进程管理   平滑重启java进程   进程信号   启动控制java服务进程   

运维经验分享作为一个专题,目前共7篇文章

  1. 运维经验分享(一)-- Linux Shell之ChatterServer服务控制脚本

  2. 运维经验分享(二)-- Linux Shell之ChatterServer服务控制脚本二次优化

  3. 运维经验分享(三)-- 解决Ubuntu下crontab不能正确执行Shell脚本的问题(一)

  4. 运维经验分享(四)--关于 java进程管理的服务控制脚本编程思路分析

  5. 运维经验分享(五)-- 改进的java进程管理的服务控制脚本

  6. 运维经验分享(六)-- 深究crontab不能正确执行Shell脚本的问题(二)

  7. 运维经验分享(七)-- Linux Shell之ChatterServer服务控制脚本第三次优化

====================================分割线======================================

针对上一篇文章《运维经验分享(四)--关于 java进程管理的服务控制脚本编程思路分析

最初版本(由别人写的):

#!/bin/sh
#chkconfig: 345 86 14
#description: Startup and shutdown script for ChatterServer
VERSION=1.0.0-snapshot
PIDFILE=./pid/chatter.pid
SERVER=./chatter-$VERSION\.jar
ARGS="-Xms2g -Xmx2g -Xmn2g -Xss128k"
# -Xms2g -Xmx2g -Xmn2g -Xss128k -XX:MaxPermSize=64m -XX:-UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=30 -XX:SurvivorRatio=6

start()
{
    if test -e $SERVER
    then
        echo "Starting $SERVER"
        if java -jar $SERVER $ARGS >./logs/console.out 2>&1 &
        then
            echo $! > $PIDFILE
            echo "server start OK"
        else
            echo "server start failed"
        fi
    else
        echo "Couldn‘t find $SERVER"
    fi
}

stop()
{
         if test -e $PIDFILE
         then
                   echo "Stopping server"
                   if kill `cat $PIDFILE` 2>./logs/console.out
                   then
                            echo "server stop OK"
                   else
                        echo "server stop faild"
                   fi
         else
                   echo "No server running"
         fi
}

restart()
{
    echo "Restarting server"
    stop
    start
}

case $1 in
         start)
                start
        ;;
         stop)
            stop
        ;;
         restart)
            restart
        ;;
         *)
            echo "Usage: $SCRIPTNAME {start|stop|restart|list}" >&2
        exit 1
        ;;
esac
exit 0

第一版本(我写的):

#!/bin/bash
#chkconfig: 345 86 14
#description: Startup and shutdown script for ChatterServer
VERSION=1.0.0-snapshot
PIDFILE=./pid/chatter.pid
SERVER=./chatter-$VERSION\.jar
ARGS="-Xms2g -Xmx4g -Xmn4g -Xss128k -XX:MaxPermSize=64m -XX:-UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=30 -XX:SurvivorRatio=6"
# -Xms2g -Xmx2g -Xmn2g -Xss128k -XX:MaxPermSize=64m -XX:-UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=30 -XX:SurvivorRatio=6

start()
{
    if test -f $PIDFILE
    then
        if test -d /proc/$(cat $PIDFILE)
        then
            echo "server has started"
            exit 1
        fi
    fi
    if test -e $SERVER
    then
        echo "Starting $SERVER"
        if java -jar $SERVER $ARGS >>./logs/console.out 2>&1 &
        then
            sleep 2
            if [[ -d /proc/$! ]]; then
                echo $! > $PIDFILE
                echo "server start OK"
            fi
        elif java -jar $SERVER $ARGS >>./logs/console.out 2>&1 &
        then
            sleep 2
            if [[ -d /proc/$! ]]; then
                echo $! > $PIDFILE
                echo "server start OK"
            fi
        else
            echo "server start failed"
            exit 1
        fi
    else
        echo "Couldn‘t find $SERVER"
        exit 1
    fi
}

stop()
{
         if test -e $PIDFILE
         then
                   echo "Stopping server"
                   if kill -TERM `cat $PIDFILE` >>./logs/console.out 2>&1
                    then
                        sleep 2
                        if test -d /proc/$(cat $PIDFILE)
                        then
                            echo "server has started"
                            sleep 60
                        fi
                        echo "server stop OK"
                        /bin/rm $PIDFILE
                   elif
                    kill -KILL `cat $PIDFILE` >>./logs/console.out 2>&1
                    then
                        sleep 2
                        if test -d /proc/$(cat $PIDFILE)
                        then
                            echo "server has started"
                            sleep 60
                        fi
                        echo "server stop OK"
                        /bin/rm $PIDFILE
                   elif
                    kill -9 `cat $PIDFILE` >>./logs/console.out 2>&1
                    then
                        sleep 2
                        if test -d /proc/$(cat $PIDFILE)
                        then
                            echo "server has started"
                            sleep 60
                        fi
                        echo "server stop OK"
                        /bin/rm $PIDFILE
                   else
                        echo "server stop failed"
                        exit 1
                   fi
         else
                   echo "No server running"
                   exit 1
         fi
}

restart()
{
    echo "Restarting server"
    stop
    start
}

case $1 in
         start)
                start
        ;;
         stop)
            stop
        ;;
         restart)
            restart
        ;;
         *)
            echo "Usage: $0 {start|stop|restart}" >&2
        exit 1
        ;;
esac
exit 0

第二版本(最后针对stop服务进程再次优化过的):

#!/bin/bash
#chkconfig: 345 86 14
#description: Startup and shutdown script for ChatterServer
VERSION=1.0.0-snapshot
PIDFILE=./pid/chatter.pid
SERVER=./chatter-$VERSION\.jar
ARGS="-Xms2g -Xmx4g -Xmn4g -Xss128k -XX:MaxPermSize=64m -XX:-UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=30 -XX:SurvivorRatio=6"
# -Xms2g -Xmx2g -Xmn2g -Xss128k -XX:MaxPermSize=64m -XX:-UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=30 -XX:SurvivorRatio=6

start()
{
    if test -f $PIDFILE
    then
        if test -d /proc/$(cat $PIDFILE)
        then
            echo "server has started"
            exit 1
        fi
    fi
    if test -e $SERVER
    then
        echo "Starting $SERVER"
        if java -jar $SERVER $ARGS >>./logs/console.out 2>&1 &
        then
            sleep 2
            if [[ -d /proc/$! ]]; then
                echo $! > $PIDFILE
                echo "server start OK"
            fi
        elif java -jar $SERVER $ARGS >>./logs/console.out 2>&1 &
        then
            sleep 2
            if [[ -d /proc/$! ]]; then
                echo $! > $PIDFILE
                echo "server start OK"
            fi
        else
            echo "server start failed"
            exit 1
        fi
    else
        echo "Couldn‘t find $SERVER"
        exit 1
    fi
}

stop()
{
         if test -e $PIDFILE
         then
                   echo "Stopping server"
                   if kill -TERM `cat $PIDFILE` >>./logs/console.out 2>&1
                    then
                        sleep 2
                        if test -d /proc/$(cat $PIDFILE)
                        then
                            sleep 60
                        else
                            echo "server stop OK"
                            /bin/rm $PIDFILE
                        fi
                   elif
                    kill -KILL `cat $PIDFILE` >>./logs/console.out 2>&1
                    then
                        sleep 2
                        if test -d /proc/$(cat $PIDFILE)
                        then
                            sleep 60
                        else
                            echo "server stop OK"
                            /bin/rm $PIDFILE
                        fi
                   elif
                    kill -9 `cat $PIDFILE` >>./logs/console.out 2>&1
                    then
                        while [[ -d /proc/$(cat $PIDFILE) ]]; do
                            sleep 1
                        done
                        if test -d /proc/$(cat $PIDFILE)
                        then
                            echo "server stop failed"
                            exit 1
                        else
                            echo "server stop OK"
                            /bin/rm $PIDFILE
                        fi
                   else
                        echo "server stop failed"
                        exit 1
                   fi
         else
                   echo "No server running"
                   exit 1
         fi
}

restart()
{
    echo "Restarting server"
    stop
    start
}

case $1 in
         start)
                start
        ;;
         stop)
            stop
        ;;
         restart)
            restart
        ;;
         *)
            echo "Usage: $0 {start|stop|restart}" >&2
        exit 1
        ;;
esac
exit 0

--end--

====================================分割线======================================

运维经验分享作为一个专题,目前共7篇文章

  1. 运维经验分享(一)-- Linux Shell之ChatterServer服务控制脚本

  2. 运维经验分享(二)-- Linux Shell之ChatterServer服务控制脚本二次优化

  3. 运维经验分享(三)-- 解决Ubuntu下crontab不能正确执行Shell脚本的问题(一)

  4. 运维经验分享(四)--关于 java进程管理的服务控制脚本编程思路分析

  5. 运维经验分享(五)-- 改进的java进程管理的服务控制脚本

  6. 运维经验分享(六)-- 深究crontab不能正确执行Shell脚本的问题(二)

  7. 运维经验分享(七)-- Linux Shell之ChatterServer服务控制脚本第三次优化

本文出自 “通信,我的最爱” 博客,请务必保留此出处http://dgd2010.blog.51cto.com/1539422/1677157

运维经验分享(五)-- 改进的java进程管理的服务控制脚本

标签:服务管理   java进程管理   平滑重启java进程   进程信号   启动控制java服务进程   

原文地址:http://dgd2010.blog.51cto.com/1539422/1677157

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!