app.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env bash
  2. # Application root directory.
  3. path=/srv/moocoder
  4. # Application username.
  5. name=moocoder-backend
  6. # Application version.
  7. version=0.4.5
  8. # Application config file. Default config.yml
  9. config_file=${path}/config.yml
  10. # File which store application process PID. Default proc.pid
  11. pid_file=${path}/proc.pid
  12. # Directory which store application log. Default ${path}/log
  13. log_dir=${path}/log
  14. # Name template of log files (Parsed with shell command "date"). Default %Y-%m-%d_%H-%M-%S.log
  15. log_name=%Y-%m-%d_%H-%M-%S.log
  16. # Time in second how long "stop" and "force-stop" wait for application totally stop. Default 60
  17. wait_count=60
  18. # JVM default time zone. Default GMT+8
  19. time_zone="GMT+8"
  20. function start {
  21. if [ -f ${pid_file} ]
  22. then
  23. local pid=$(cat ${pid_file})
  24. if ps -p ${pid} > /dev/null
  25. then
  26. echo "Application ${name} is already running! (Version:${version} PID:${pid})"
  27. else
  28. rm -rf ${pid_file}
  29. fi
  30. fi
  31. if [ ! -f ${pid_file} ]
  32. then
  33. if [ ! -d ${log_dir} ]
  34. then
  35. mkdir ${log_dir}
  36. fi
  37. java -server -jar ${path}/${name}-${version}.jar --spring.config.additional-location=${config_file} -Duser.timezone=${time_zone} &>${log_dir}/$(date +${log_name}) &
  38. local pid=$(echo -e "$!\c")
  39. echo -e "${pid}\c" > ${pid_file}
  40. echo "Start application ${name} successfully! (Version:${version} PID:${pid})"
  41. fi
  42. }
  43. function stop {
  44. if [ -f ${pid_file} ]
  45. then
  46. if ! ps -p $(cat ${pid_file}) > /dev/null
  47. then
  48. rm -rf ${pid_file}
  49. fi
  50. fi
  51. if [ -f ${pid_file} ]
  52. then
  53. local pid=$(cat ${pid_file})
  54. kill ${pid}
  55. local count=${wait_count}
  56. while((${count} > 0))
  57. do
  58. sleep 1
  59. if ! ps -p ${pid} > /dev/null
  60. then
  61. rm -rf ${pid_file}
  62. echo "Stop application ${name} successfully! (Version:${version})"
  63. break
  64. fi
  65. count=`expr ${count} - 1`
  66. done
  67. if [ ${count} -eq 0 ]
  68. then
  69. echo "Failed to stop application ${name}! (Version:${version} PID:${pid})"
  70. exit 1
  71. fi
  72. else
  73. echo "Application ${name} is not running! (Version:${version})"
  74. fi
  75. }
  76. function restart {
  77. stop
  78. start
  79. }
  80. function status {
  81. if [ -f ${pid_file} ]
  82. then
  83. if ! ps -p $(cat ${pid_file}) > /dev/null
  84. then
  85. rm -rf ${pid_file}
  86. fi
  87. fi
  88. if [ -f ${pid_file} ]
  89. then
  90. local pid=$(cat ${pid_file})
  91. echo "Application ${name} is running! (Version:${version} PID:${pid})"
  92. else
  93. echo "Application ${name} is not running! (Version:${version})"
  94. fi
  95. }
  96. function usage {
  97. echo "Usage: $0 {start|stop|restart|status}"
  98. }
  99. pushd ${path} > /dev/null
  100. case $1 in
  101. start)
  102. start
  103. ;;
  104. stop)
  105. stop
  106. ;;
  107. restart)
  108. restart
  109. ;;
  110. status)
  111. status
  112. ;;
  113. *)
  114. usage
  115. ;;
  116. esac
  117. popd > /dev/null
  118. exit 0