Spring Boot 应用,自定义启停 Java 进程脚本。
完整脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #!/bin/bash # 需要变更的参数 # 先查看java绝对路径:which java java=/usr/bin/java STARTUP="$java -server -Xms1024m -Xmx1024m -jar " PROJECT="pre-diagnose/manager" # Jar 目录 JAR=`ls /home/$PROJECT/*.jar -th |head -1`
cd /home/$PROJECT
function start () { nohup $STARTUP $JAR >> /home/$PROJECT/logs/manager.log 2>&1 & exit 0 }
function stop () { PID=`ps aux|grep "$JAR"|grep -v grep|awk '{print $2}'` if [ -z $PID ];then exit 0 else kill -9 $PID fi }
function reload () { PID=`ps aux|grep "$JAR"|grep -v grep|awk '{print $2}'` if [ -z $PID ];then exit 0 else kill -1 $PID fi } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop sleep 3 start ;; *) echo "start|stop|reload|restart" ;; esac
|
脚本说明
查看进程并输出进程号
1
| ps aux|grep "$JAR"|grep -v grep|awk '{print $2}'
|
grep -v
:排除输出
awk '{print $2}'
:输出第二列的值
启动脚本
1 2
| # 堆最小为128m,最大为128m nohup java -jar -Xms128m -Xmx128m conv-manager-provider.jar > manager.log 2>&1 &
|
停止脚本
1
| kill -15 `ps -aux|grep conv-manager-provider.jar|grep -v grep|awk '{print $2}'`
|