Linux中Java应用程序jar包启动、停止、重启脚本

新建启动脚本文件start.sh ,脚本内容如下:

  1. #!/bin/bash
  2. # 激活的Spring配置文件
  3. SPRING_PROFILE="-Dspring.profiles.active=prod"
  4. # 应用程序,全路径加jar包名
  5. APP_NAME="/xxx/xxx/xxx.jar"
  6. # 虚拟机参数,根据需求添加修改
  7. JVM="-Xms256m -Xmx2048m"
  8. # 远程Debug调试配置
  9. REMOTE_CONFIG="-Xdebug -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=n"
  10. # 使用说明
  11. function usage() {
  12. echo "使用说明:sh 脚本名称.sh [start | stop | restart | debug | status]"
  13. exit 1
  14. }
  15. # 检查程序是否在运行
  16. function is_exist() {
  17. pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
  18. # 如果不存在则返回1,存在则返回0
  19. if [ -z "${pid}" ]; then
  20. return 1
  21. else
  22. return 0
  23. fi
  24. }
  25. # 启动应用
  26. function start(){
  27. is_exist
  28. if [ $? -eq "0" ]; then
  29. echo "${APP_NAME} is already running. pid=${pid}"
  30. else
  31. nohup java ${JVM} ${SPRING_PROFILE} -jar ${APP_NAME} >/dev/null 2>&1 &
  32. echo "${APP_NAME} start success"
  33. fi
  34. }
  35. # 停止应用
  36. function stop(){
  37. is_exist
  38. if [ $? -eq "0" ]; then
  39. echo "${APP_NAME} shutting down. pid=${pid}"
  40. kill -9 $pid
  41. else
  42. echo "${APP_NAME} is not running"
  43. fi
  44. }
  45. # 输出应用运行状态
  46. function status(){
  47. is_exist
  48. if [ $? -eq "0" ]; then
  49. echo "${APP_NAME} is running. pid=${pid}"
  50. else
  51. echo "${APP_NAME} is not running"
  52. fi
  53. }
  54. # 重启
  55. function restart(){
  56. stop
  57. start
  58. }
  59. # 远程调试Debug模式
  60. function debug() {
  61. echo " start remote debug mode .........."
  62. nohup java ${REMOTE_CONFIG} -jar $APP_NAME >/dev/null 2>&1 &
  63. }
  64. # 根据输入参数,选择执行对应方法,不输入则执行使用说明
  65. case "$1" in
  66. "start")
  67. start
  68. ;;
  69. "debug")
  70. debug
  71. ;;
  72. "stop")
  73. stop
  74. ;;
  75. "restart")
  76. restart
  77. ;;
  78. "status")
  79. status
  80. ;;
  81. *)
  82. usage
  83. ;;
  84. esac
  85. exit 0

启动应用 sh start.sh start

停止应用 sh start.sh stop

重启应用 sh start.sh restart

查看状态 sh start.sh status

远程debug模式启动应用 sh start.sh debug


© 2020-2025 www.chenhuazhan.com All Rights Reserved 备案号:桂ICP备17004487号-1 粤公网安备44030002005146