需要配置端口号和jar包名称,如下为8080和web.jar
自动获取当前路径,jar包放在同脚本目录下的版本目录(eg:1.0.0)中,
自动使用kill-15自动杀死对应端口的进程,5秒后未杀死可以使用kill-9强制杀死进程,
最后选择性输出启动日志。
脚本如下:
#!/bin/bash# 读取端口号port=8080baseDir=$(pwd)dir=$1project="web.jar"# 确认端口号echo "[ warn] Is the port($port) right? [Y/N]"read isPortOkif [ "$isPortOk" != "Y" ]; thenecho '[ info] exit.'exit 0;fiecho '[ info] port is '$port# 输入 jar 所在文件夹名称if [ "$dir" == "" ]; thenecho "[ warn] input dir name. eg: 1.0.1"read dirif [ "$dir" == "" ]; thenecho '[ error] exit.'exit 0;fifi# 绝对路径fullFilePath=$baseDir/$dir/$projectlogFilePath="$baseDir/$dir/log.out"# 确认 jar包位置echo "[ warn] Is project right, path is $fullFilePath? [Y/N]"read isProjectPathOkif [ "$isProjectPathOk" != "Y" ]; thenecho '[ info] exit.'exit 0;fi# 校验文件是否存在if [ ! -f "$fullFilePath" ];thenecho "[ error] project file not exist.";echo '[ info] exit.'exit 0;figetPID() {curPid=$(netstat -nlp|grep $1 | awk '{print $7}' | awk -F"/" '{print $1}');echo $curPid}startProject() {echo "[ warn] Is start project? [Y/N]"read isStartif [ "$isStart" != "Y" ]; thenecho '[ info] exit.'exit 0;fiecho '[ info] start project, waiting....'#nohup java -jar $fullFilePath > $logFilePath 2>&1 &nohup java -jar -server -Xms512m -Xmx512m -XX:CompressedClassSpaceSize=128m -XX:MetaspaceSize=200m -XX:MaxMetaspaceSize=200m $fullFilePath > $logFilePath 2>&1 &echo "[ info] logger path is $logFilePath"echo "[ info] Is show logger? [Y/N]"read isShowLoggerif [ "$isShowLogger" != "Y" ]; thenecho '[ info] exit.'exit 0;fitail -f $logFilePathecho '[ info] start success.'}echo '[ info] '$(netstat -nlp|grep :$port)# 第一次 kill -15pid=$(getPID $port);if [ $pid ]; thenecho "[ info] pid is $pid"echo "[ warn] run kill -15 $pid? [Y/N]"read isKillPidif [ "$isKillPid" != "Y" ]; thenecho '[ info] exit.'exit 0;fiecho '[ info] killing, waiting....'kill -15 $pidelseecho '[ info] first: no pid, will start.'# 调用启动方法startProject# 启动后退出exit 0fisleep 5# 第二次 kill -9, 第一次 没杀掉pid=$(getPID $port);if [ $pid ]; thenecho "[ info] pid is $pid"echo "[ warn] run kill -9 $pid? [Y/N]"read isKillif [ "$isKill" != "Y" ]; thenecho '[ info] exit.'exit 0;fiecho '[ info] killing, waiting....'kill -9 $pidelseecho '[ info] second: no pid, will start.'# 调用启动方法startProject# 启动后退出exit 0fisleep 5# 第三次获取 pid 判断是否kill掉进程pid=$(getPID $port);if [ $pid ]; thenecho '[ warn] last: PID is '$pidecho '[ error] last: This process cannot be killed. pid is' $pidelseecho '[ info] last: no pid, will start.'# 调用启动方法startProject# 启动后退出exit 0fi
