linux 编程 Windows centos shell wordpress java nginx mysql 程序员 Android Ubuntu google Python apache 微软 Firefox 开源 云计算 php

運維經驗分享(五)– 改進的java進程管理的服務控制腳本

针对上一篇文章《运维经验分享(四)--关于 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--

延伸阅读

评论