运维经验分享(五)-- 改进的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进程管理的服务控制脚本编程思路分析

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/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

第一版本(我写的):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/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服务进程再次优化过的):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/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服务控制脚本第三次优化



本文转自 urey_pp 51CTO博客,原文链接:http://blog.51cto.com/dgd2010/1677157,如需转载请自行联系原作者



上一篇:SDN和物联网促使网络产生巨大结构性变革 IoT是企业增收关键


下一篇:《STM32库开发实战指南:基于STM32F103(第2版)》——1.2节获取KEIL5安装包