boot.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/bin/sh
  2. PROCESS_NAME=${serviceName}
  3. MAIN_CLASS=${mainClass}
  4. cd `dirname $0`
  5. BIN_DIR=`pwd`
  6. #set -x
  7. #set -e
  8. PROCESS_HOME=`dirname $BIN_DIR`
  9. LOG_PATH=$PROCESS_HOME/log
  10. if [ -n "$2" ];then
  11. PROCESS_NAME=$2;
  12. fi
  13. if [ -n "$3" ];then
  14. MAIN_CLASS=$3;
  15. fi
  16. if [ -z "$JAVA_HOME" ];then
  17. echo "JAVA_HOME NOT SET"
  18. exit -1
  19. fi
  20. LIB_DIR=`dirname $PROCESS_HOME`/lib
  21. JARS="$PROCESS_HOME/prjConf:$PROCESS_HOME/lib/*:"
  22. JARS="$JARS$LIB_DIR/*"
  23. # only *; *.jar is not support
  24. #if [ -d $LIB ]; then
  25. # for i in $LIB/*.jar; do
  26. # JARS="$JARS":$i
  27. # done
  28. #fi
  29. export JARS
  30. if [ ! -d "$BIN_DIR/process/" ];then
  31. mkdir $BIN_DIR/process
  32. fi
  33. if [ ! -d "$PROCESS_HOME/log/" ];then
  34. mkdir $PROCESS_HOME/log
  35. touch $PROCESS_HOME/log/console.log
  36. fi
  37. JAVA_OPTS="-Dprogram.name=$PROCESS_NAME -DPROCESS_HOME=$PROCESS_HOME -Dlog.path=$LOG_PATH -Xms256M -Xmx1G -Duser.timezone=GMT+8 -Dfile.encoding=UTF-8"
  38. JAVA_OPTS="$JAVA_OPTS -Xloggc:$BIN_DIR/process/gc.vgc -XX:+PrintGCDateStamps"
  39. #进程ID
  40. jpsid=0
  41. #检测进程
  42. checkpid(){
  43. javaps=`jps -lmv|grep $PROCESS_NAME|awk '{print $1}'`
  44. if [ -n "$javaps" ];then
  45. jpsid=$javaps
  46. else
  47. jpsid=0
  48. fi
  49. }
  50. #启动
  51. start(){
  52. checkpid
  53. if [ $jpsid -ne 0 ]; then
  54. echo "================================"
  55. echo "warn: $PROCESS_NAME already started! (pid=$psid)"
  56. echo "================================"
  57. else
  58. echo -n "Starting $PROCESS_NAME ..."
  59. nohup java -server $JAVA_OPTS -cp $JARS $MAIN_CLASS > $PROCESS_HOME/log/console.log 2>&1 &
  60. sleep 2
  61. checkpid
  62. if [ $jpsid -ne 0 ]; then
  63. echo "(pid=$jpsid) [OK]"
  64. else
  65. echo "[Failed]"
  66. fi
  67. fi
  68. }
  69. #停止
  70. stop() {
  71. checkpid
  72. if [ $jpsid -ne 0 ]; then
  73. echo -n "Stopping $PROCESS_NAME ...(pid=$jpsid) "
  74. kill $1 $jpsid
  75. if [ $? -eq 0 ]; then
  76. echo "[OK]"
  77. else
  78. echo "[Failed]"
  79. fi
  80. sleep 2
  81. checkpid
  82. if [ $jpsid -ne 0 ]; then
  83. stop -9
  84. fi
  85. else
  86. echo "================================"
  87. echo "warn: $PROCESS_NAME is not running"
  88. echo "================================"
  89. fi
  90. }
  91. #状态
  92. status() {
  93. checkpid
  94. if [ $jpsid -ne 0 ]; then
  95. echo "$PROCESS_NAME is running! (pid=$jpsid)"
  96. tail $PROCESS_HOME/log/gihon.log
  97. else
  98. echo "$PROCESS_NAME is not running"
  99. fi
  100. }
  101. #帮助信息
  102. info() {
  103. echo "************************************************************************"
  104. echo "JAVA_HOME=$JAVA_HOME"
  105. echo `java -version`
  106. echo "gihon service v1.0.0"
  107. echo "usage: ./boot.sh <command>"
  108. echo "command = < start|stop|info|restart|status|help >"
  109. echo "start: start service '$2' and MainClass '$3' in current shell, and in background if succeded with &."
  110. echo "the program should be execute with root privelage."
  111. echo "***************************************************************************"
  112. }
  113. case "$1" in
  114. 'start')
  115. start
  116. ;;
  117. 'stop')
  118. stop
  119. ;;
  120. 'status')
  121. status
  122. ;;
  123. 'restart')
  124. stop
  125. start
  126. ;;
  127. *)
  128. info
  129. ;;
  130. esac