需要配置端口号和jar包名称,如下为8080和web.jar
    自动获取当前路径,jar包放在同脚本目录下的版本目录(eg:1.0.0)中,
    自动使用kill-15自动杀死对应端口的进程,5秒后未杀死可以使用kill-9强制杀死进程,
    最后选择性输出启动日志。
    脚本如下:

    1. #!/bin/bash
    2. # 读取端口号
    3. port=8080
    4. baseDir=$(pwd)
    5. dir=$1
    6. project="web.jar"
    7. # 确认端口号
    8. echo "[ warn] Is the port($port) right? [Y/N]"
    9. read isPortOk
    10. if [ "$isPortOk" != "Y" ]; then
    11. echo '[ info] exit.'
    12. exit 0;
    13. fi
    14. echo '[ info] port is '$port
    15. # 输入 jar 所在文件夹名称
    16. if [ "$dir" == "" ]; then
    17. echo "[ warn] input dir name. eg: 1.0.1"
    18. read dir
    19. if [ "$dir" == "" ]; then
    20. echo '[ error] exit.'
    21. exit 0;
    22. fi
    23. fi
    24. # 绝对路径
    25. fullFilePath=$baseDir/$dir/$project
    26. logFilePath="$baseDir/$dir/log.out"
    27. # 确认 jar包位置
    28. echo "[ warn] Is project right, path is $fullFilePath? [Y/N]"
    29. read isProjectPathOk
    30. if [ "$isProjectPathOk" != "Y" ]; then
    31. echo '[ info] exit.'
    32. exit 0;
    33. fi
    34. # 校验文件是否存在
    35. if [ ! -f "$fullFilePath" ];then
    36. echo "[ error] project file not exist.";
    37. echo '[ info] exit.'
    38. exit 0;
    39. fi
    40. getPID() {
    41. curPid=$(netstat -nlp|grep $1 | awk '{print $7}' | awk -F"/" '{print $1}');
    42. echo $curPid
    43. }
    44. startProject() {
    45. echo "[ warn] Is start project? [Y/N]"
    46. read isStart
    47. if [ "$isStart" != "Y" ]; then
    48. echo '[ info] exit.'
    49. exit 0;
    50. fi
    51. echo '[ info] start project, waiting....'
    52. #nohup java -jar $fullFilePath > $logFilePath 2>&1 &
    53. nohup java -jar -server -Xms512m -Xmx512m -XX:CompressedClassSpaceSize=128m -XX:MetaspaceSize=200m -XX:MaxMetaspaceSize=200m $fullFilePath > $logFilePath 2>&1 &
    54. echo "[ info] logger path is $logFilePath"
    55. echo "[ info] Is show logger? [Y/N]"
    56. read isShowLogger
    57. if [ "$isShowLogger" != "Y" ]; then
    58. echo '[ info] exit.'
    59. exit 0;
    60. fi
    61. tail -f $logFilePath
    62. echo '[ info] start success.'
    63. }
    64. echo '[ info] '$(netstat -nlp|grep :$port)
    65. # 第一次 kill -15
    66. pid=$(getPID $port);
    67. if [ $pid ]; then
    68. echo "[ info] pid is $pid"
    69. echo "[ warn] run kill -15 $pid? [Y/N]"
    70. read isKillPid
    71. if [ "$isKillPid" != "Y" ]; then
    72. echo '[ info] exit.'
    73. exit 0;
    74. fi
    75. echo '[ info] killing, waiting....'
    76. kill -15 $pid
    77. else
    78. echo '[ info] first: no pid, will start.'
    79. # 调用启动方法
    80. startProject
    81. # 启动后退出
    82. exit 0
    83. fi
    84. sleep 5
    85. # 第二次 kill -9, 第一次 没杀掉
    86. pid=$(getPID $port);
    87. if [ $pid ]; then
    88. echo "[ info] pid is $pid"
    89. echo "[ warn] run kill -9 $pid? [Y/N]"
    90. read isKill
    91. if [ "$isKill" != "Y" ]; then
    92. echo '[ info] exit.'
    93. exit 0;
    94. fi
    95. echo '[ info] killing, waiting....'
    96. kill -9 $pid
    97. else
    98. echo '[ info] second: no pid, will start.'
    99. # 调用启动方法
    100. startProject
    101. # 启动后退出
    102. exit 0
    103. fi
    104. sleep 5
    105. # 第三次获取 pid 判断是否kill掉进程
    106. pid=$(getPID $port);
    107. if [ $pid ]; then
    108. echo '[ warn] last: PID is '$pid
    109. echo '[ error] last: This process cannot be killed. pid is' $pid
    110. else
    111. echo '[ info] last: no pid, will start.'
    112. # 调用启动方法
    113. startProject
    114. # 启动后退出
    115. exit 0
    116. fi