话不多说、上代码
#!/bin/bash# JAR存放全路径
PROJECT_JAR_HOME='/home/project/linsn_box'
PROJECT_NAME_JAR='zhangsan-admin.jar'# 执行启动操作
start(){
# 切换到JAR项目目录
cd $PROJECT_JAR_HOME
# 执行启动命令
nohup java -jar $PROJECT_NAME_JAR > /dev/null 2>&1 &
# 输出已执行启动完成
echo The project has been initiated and completed
}# 执行停止操作
stop(){# 查询正在运行的pid
A_PID=$(ps -ef | grep $PROJECT_NAME_JAR | grep -v grep | awk '{print $2}')# 判断是否为空
if [ -z "$A_PID" ]then# 如果为空 则表示项目已停止或未启动echo The project has been stopped or not started yetelse# 如果不为空则执行停止操作echo The project is currently runningkill -9 $A_PIDecho The project has executed the stop command
fi
}# 执行查看操作
status(){# 查询正在运行的pid
B_PID=$(ps -ef | grep $PROJECT_NAME_JAR | grep -v grep)# 判断是否为空if [ -z "$B_PID" ]
then# 如果为空 则表示项目已停止或未启动echo The project cannot be found
elseecho The project is currently running pid:$B_PID
fi
}case $1 instart)start
;;
stop)stop
;;
status)status
;;
restart)$0 stopsleep 5$0 start
;;
*)echo Can only be executed {start|stop|restart}
;;
esac
exit 0
以上代码重命名为 project-opt.sh 即可(前提条件必须已安装java环境 通过java -version可查看JDK版本)
通过
xxx.sh start/stop/status 启动/停止/查看状态
本人亲测有效。