--- title: Spring boot部署脚本 date: 2017-06-21 13:45:28 updated: 2017-06-21 13:45:28 description: "Linux下运行jar的脚本" categories: [技术总结] tags: [Linux, Java, Spring boot, Shell] --- ## 简介 > Spring boot程序需要打成jar包在Linux上后台运行,维护比较困难,所以找到了以下脚本,可以存储java程序的pid到文件中去,方便停止和查看状态 > **注意:**`这是比较原始的部署方式了,现在已经使用Docker方式部署了`,详见另一篇博文:[多环境jar部署脚本示例](/2018/03/26/shell-scripts) ### start.sh ```bash #!/bin/sh rm -f tpid nohup java -jar -Dspring.profiles.active=profileName -Xms512m -Xmx2048m myapp.jar --spring.config.location=application.yml > /dev/null 2>&1 &echo $! > tpid echo Start Success! ``` ### stop.sh ```bash #!/bin/sh APP_NAME=myapp tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'Stop Process...' kill -15 $tpid fi sleep 5 tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'Kill Process!' kill -9 $tpid else echo 'Stop Success!' fi ``` ### check.sh ```bash #!/bin/sh APP_NAME=myapp tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'App is running.' else echo 'App is NOT running.' fi ``` ### kill.sh ```bash #!/bin/sh APP_NAME=myapp tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'Kill Process!' kill -9 $tpid fi ```