#!/bin/bash
#description: 启动重启 jar 服务
#端口号,根据此端口号确定PID
PORT=7777
#启动命令所在目录
#HOME='/usr/etc/server/java/server'
#查询出监听了PORT端口TCP协议的程序
#pid=`netstat -anp|grep $PORT|awk '{printf $7}'|cut -d/ -f1`
pid=`lsof -i:$PORT|awk '{printf $2}'|cut -d/ -f1`
#截取字符串,只需要pid
pid=${pid:3}
#jar名称
jarName=project-0.0.1-SNAPSHOT.jar
#active
active=product
#启动
start(){
if [ -n "$pid" ]; then
echo "server already start,pid:$pid"
return 0
fi
#进入命令所在目录
#cd $HOME
#启动 把控制台日志输出到console.log文件中
nohup java -jar $jarName --spring.profiles.active=$active > console.log 2>&1 &
echo "start at port:$PORT"
}
#启动带创建视图
startView(){
if [ -n "$pid" ]; then
echo "server already start,pid:$pid"
return 0
fi
#进入命令所在目录
#cd $HOME
#启动 把控制台日志输出到console.log文件中
nohup java -jar $jarName --spring.profiles.active=$active > console.log 2>&1 &
echo "start at port:$PORT"
}
#停止
stop(){
if [ -z "$pid" ]; then
echo "not find program on port:$PORT"
return 0
fi
#结束程序,使用讯号2,如果不行可以尝试讯号9强制结束
kill -9 $pid
rm -rf $pid
echo "kill program use signal 2,pid:$pid"
}
#状态
status(){
if [ -z "$pid" ]; then
echo "not find program on port:$PORT"
else
echo "program is running,pid:$pid"
fi
}
#查看进程
psf(){
ps -ef | grep $jarName
}
#执行参数
case $1 in
start)
start
;;
view)
startView
;;
stop)
stop
;;
restart)
$0 stop
sleep 2
$0 start
;;
status)
status
;;
ps)
psf
;;
help)
echo "Usage: {start|stop|restart|status|ps}"
;;
esac
exit 0