service.sh 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash
  2. #设置jar文件名
  3. APP_NAME=app.jar
  4. #使用说明,用来提示输入参数
  5. usage() {
  6. echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
  7. exit 1
  8. }
  9. #检查程序是否在运行
  10. is_exist(){
  11. pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
  12. #如果不存在返回1,存在返回0
  13. if [ -z "${pid}" ]; then
  14. return 1
  15. else
  16. return 0
  17. fi
  18. }
  19. #启动方法
  20. start(){
  21. is_exist
  22. if [ $? -eq "0" ]; then
  23. echo "${APP_NAME} is already running. pid=${pid} ."
  24. else
  25. nohup java -Xms1024m -Xmx1024m -jar $APP_NAME > /dev/null 2>&1 &
  26. fi
  27. }
  28. #停止方法
  29. stop(){
  30. is_exist
  31. if [ $? -eq "0" ]; then
  32. kill -9 $pid
  33. else
  34. echo "${APP_NAME} is not running"
  35. fi
  36. }
  37. #输出运行状态
  38. status(){
  39. is_exist
  40. if [ $? -eq "0" ]; then
  41. echo "${APP_NAME} is running. Pid is ${pid}"
  42. else
  43. echo "${APP_NAME} is NOT running."
  44. fi
  45. }
  46. #重启
  47. restart(){
  48. stop
  49. start
  50. }
  51. #根据输入参数,选择执行对应方法,不输入则执行使用说明
  52. case "$1" in
  53. "start")
  54. start
  55. ;;
  56. "stop")
  57. stop
  58. ;;
  59. "status")
  60. status
  61. ;;
  62. "restart")
  63. restart
  64. ;;
  65. *)
  66. usage
  67. ;;
  68. esac