runner.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #######################################################################################################################
  2. # Java 应用进程管理脚本
  3. #######################################################################################################################
  4. #!/bin/bash
  5. jarfile_name="spiderlab-dagent.jar"
  6. # 启动进程
  7. start() {
  8. pid=`jps | grep ${jarfile_name} | awk '{print $1}'`
  9. if [ ! -z ${pid} ]
  10. then
  11. echo "${jarfile_name} has been running with pid ${pid}..."
  12. exit 0
  13. fi
  14. nohup java -jar ${jarfile_name} > console.log 2>&1 &
  15. declare count=0
  16. declare max=60
  17. while [[ ${count} -lt ${max} && -z ${pid} ]]; do
  18. ((count++))
  19. echo "sleep ${count}s to wait ${jarfile_name} start..."
  20. sleep 1
  21. pid=`jps | grep ${jarfile_name} | awk '{print $1}'`
  22. done
  23. if [ ${count} -gt ${max} ]; then
  24. echo "${jarfile_name} started failed..."
  25. exit 1
  26. else
  27. echo "${jarfile_name} has been started with pid ${pid}..."
  28. exit 0
  29. fi
  30. }
  31. # 结束进程
  32. stop() {
  33. pid=`jps | grep ${jarfile_name} | awk '{print $1}'`
  34. if [ -z ${pid} ]
  35. then
  36. echo "${jarfile_name} not running..."
  37. else
  38. kill -15 ${pid}
  39. oldpid=${pid}
  40. declare count=0
  41. declare max=60
  42. while [[ ${count} -lt ${max} && ! -z ${pid} ]]; do
  43. ((count++))
  44. echo "sleep ${count}s to wait ${jarfile_name} shutdown..."
  45. sleep 1
  46. pid=`jps | grep ${jarfile_name} | awk '{print $1}'`
  47. done
  48. if [ ${count} -gt ${max} ]; then
  49. echo "${jarfile_name} shutdown failed..."
  50. exit 1
  51. else
  52. echo "${jarfile_name} with pid ${oldpid} has been killed..."
  53. exit 0
  54. fi
  55. fi
  56. }
  57. case $1 in
  58. start)
  59. start
  60. ;;
  61. stop)
  62. stop
  63. ;;
  64. restart)
  65. stop
  66. start
  67. ;;
  68. *)
  69. echo "Usage: sh runner.sh [start|stop|restart]"
  70. exit 1
  71. ;;
  72. esac