看到比较好的linux脚本:
start.sh
#!/bin/shrm -f tpidnohup java -jar xx.jar --spring.profiles.active=dev > /dev/null 2>&1 &echo $! > tpidecho Start Success!
stop.sh
#!/bin/shAPP_NAME=myapptpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`if [ ${tpid} ]; then echo 'Stop Process...' kill -15 $tpidfisleep 5tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`if [ ${tpid} ]; then echo 'Kill Process!' kill -9 $tpidelse echo 'Stop Success!'fi
check.sh
#!/bin/shAPP_NAME=myapptpid=`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
#!/bin/shAPP_NAME=myapptpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`if [ ${tpid} ]; then echo 'Kill Process!' kill -9 $tpidfi
以上合并为一个脚本:
#!/bin/shAPP=stockhouseAPP_NAME=${APP}".jar"command=$1function start(){ rm -f tpid nohup java -jar ${APP_NAME} > /dev/null 2>&1 & echo $! > tpid check}function stop(){ 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}function check(){ 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}function forcekill(){ 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}if [ "${command}" == "start" ]; then startelif [ "${command}" == "stop" ]; then stopelif [ "${command}" == "check" ]; then checkelif [ "${command}" == "kill" ]; then forcekillelse echo "Unknow argument...."fi