app.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. #JVM参数
  3. JVM_OPTS="-Dname=$AppName -Duser.timezone=Asia/Shanghai -Xms512M -Xmx512M -XX:PermSize=256M -XX:MaxPermSize=512M -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"
  4. APP_HOME=`pwd`
  5. echo $APP_HOME
  6. AppName="$2"
  7. #输出日志目录
  8. LOG_PATH=$APP_HOME/logs
  9. #日志目录不存在就创建
  10. if [ ! -d $LOG_PATH ];then
  11. mkdir -p $LOG_PATH
  12. fi
  13. #输出日志文件
  14. LOG_FILE=$LOG_PATH/$AppName.log
  15. echo "log: $LOG_FILE"
  16. if [ "$1" = "" ];
  17. then
  18. echo -e "\033[0;31m 未输入操作名 \033[0m \033[0;34m {start|stop|restart|status} \033[0m"
  19. exit 1
  20. fi
  21. if [ "$AppName" = "" ];
  22. then
  23. echo -e "\033[0;31m 未输入应用名 \033[0m"
  24. exit 1
  25. fi
  26. if [ ! -e $AppName.jar ]; then
  27. echo -e "\033[0;31m 应用的 jar 文件($AppName.jar)不存在 \033[0m"
  28. exit 1
  29. fi
  30. function start()
  31. {
  32. PID=`ps -ef |grep java|grep $AppName|grep -v grep|awk '{print $2}'`
  33. if [ x"$PID" != x"" ]; then
  34. echo "$AppName is running..."
  35. else
  36. #nohup java -jar $JVM_OPTS $AppName > /dev/null 2>&1 &
  37. nohup java -jar $JVM_OPTS $AppName.jar > $LOG_FILE 2>&1 &
  38. echo "Start $AppName success..."
  39. fi
  40. }
  41. function stop()
  42. {
  43. echo "Stop $AppName"
  44. PID=""
  45. query(){
  46. PID=`ps -ef |grep java|grep $AppName|grep -v grep|awk '{print $2}'`
  47. }
  48. query
  49. if [ x"$PID" != x"" ]; then
  50. kill -TERM $PID
  51. echo "$AppName (pid:$PID) exiting..."
  52. while [ x"$PID" != x"" ]
  53. do
  54. sleep 1
  55. query
  56. done
  57. echo "$AppName exited."
  58. else
  59. echo "$AppName already stopped."
  60. fi
  61. }
  62. function restart()
  63. {
  64. stop
  65. sleep 2
  66. start
  67. }
  68. function status()
  69. {
  70. PID=`ps -ef |grep java|grep $AppName|grep -v grep|wc -l`
  71. if [ $PID != 0 ];then
  72. echo "$AppName is running..."
  73. else
  74. echo "$AppName is not running..."
  75. fi
  76. }
  77. case $1 in
  78. start)
  79. start;;
  80. stop)
  81. stop;;
  82. restart)
  83. restart;;
  84. status)
  85. status;;
  86. *)
  87. esac