run.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env bash
  2. # sh run.sh
  3. # app应用启动脚本
  4. # JAVA_HOME: java目录
  5. # APP_HOME: app应用的目录
  6. # APP_JAR: app应用打包后jar名
  7. # APP_WORK: app应用工作目录
  8. # JAVA_OPTS: JAVA启动参数
  9. JAVA_HOME=
  10. APP_HOME=
  11. APP_JAR=
  12. APP_WORK="$APP_HOME/work"
  13. # JAVA进程启动参数
  14. # 启动打印VM与默认不一样参数 -XX:+PrintCommandLineFlags
  15. # 性能相关 -XX:-UseBiasedLocking -XX:-UseCounterDecay -XX:AutoBoxCacheMax=20000
  16. # 内存相关 -Xmx1024m -Xms1024m -Xmn256m -XX:PermSize=128m -XX:MaxPermSize=256m -XX:MaxDirectMemorySize=512m -XX:ReservedCodeCacheSize=100M
  17. # CMS GC相关 -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:MaxTenuringThreshold=6 -XX:+ExplicitGCInvokesConcurrent -XX:+ParallelRefProcEnabled
  18. # GC相关 -Xloggc:/dev/shm/app-gc.log -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps -XX:+PrintGCDetails
  19. # 异常日志相关 -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${LOGDIR}/ -XX:ErrorFile=${LOGDIR}/hs_err_%p.log
  20. # JMX相关 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=7200 -Dcom.sun.management.jmxremote.rmi.port=7200 -Djava.rmi.server.hostname=192.168.1.2 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.acccess.file=./management/jmxremote.access -Dcom.sun.management.jmxremote.password.file=./management/jmxremote.password
  21. JAVA_OPTS=" -server -Xmx1024m -Xms1024m -Duser.timezone=GMT+08 -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxTenuringThreshold=15 -Xloggc:$APP_WORK/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$APP_WORK/dump/"
  22. # JAVA进程IP, 全局变量
  23. psid=0
  24. checkpid() {
  25. javaps=`$JAVA_HOME/bin/jps -l | grep "\<${APP_JAR}\>"`
  26. if [ -n "$javaps" ]; then
  27. psid=`echo $javaps | awk '{print $1}'`
  28. else
  29. psid=0
  30. fi
  31. }
  32. # 启动应用
  33. start() {
  34. checkpid
  35. if [ $psid -ne 0 ]; then
  36. echo "================================"
  37. echo "warn: $APP_JAR already started! (pid=$psid)"
  38. echo "================================"
  39. else
  40. echo -n "Starting $APP_JAR ..."
  41. nohup $JAVA_HOME/bin/java $JAVA_OPTS -jar $APP_JAR >$APP_WORK/nohup.out 2>&1 &
  42. checkpid
  43. if [ $psid -ne 0 ]; then
  44. echo "(pid=$psid) [OK]"
  45. else
  46. echo "[Failed]"
  47. fi
  48. fi
  49. }
  50. # 停止应用
  51. stop() {
  52. checkpid
  53. if [ $psid -ne 0 ]; then
  54. echo -n "Stopping $APP_JAR ...(pid=$psid) "
  55. kill -15 $psid
  56. if [ $? -eq 0 ]; then
  57. echo "[OK]"
  58. else
  59. echo "[Failed]"
  60. fi
  61. sleep 1
  62. checkpid
  63. if [ $psid -ne 0 ]; then
  64. stop
  65. fi
  66. else
  67. echo "================================"
  68. echo "warn: $APP_JAR is not running"
  69. echo "================================"
  70. fi
  71. }
  72. case $1 in
  73. start)
  74. echo "start project......"
  75. start
  76. ;;
  77. stop)
  78. echo "stop project......"
  79. stop
  80. ;;
  81. restart)
  82. echo "restart project......"
  83. stop
  84. start
  85. ;;
  86. *)
  87. esac
  88. exit 0