啟動停止多個jar
阿新 • • 發佈:2021-12-24
啟動停止多個jar
啟動檔案設定
多檔案同時啟動
gollum.sh
#!/bin/sh #chkconfig: 2345 80 05 #description: gollum ##jar包 export EUREKA=/root/gollum/jar/server-eureka-1.0.jar export ZUUL=/root/gollum/jar/server-zuul-1.0.jar export LAB=/root/gollum/jar/service-laboratory-1.0.jar export EXAM=/root/gollum/jar/service-examination-1.0.jar export CONSULT=/root/gollum/jar/service-consultation-1.0.jar ##埠 export EUREKA_port=8000 export ZUUL_port=8080 export LAB_port=9106 export EXAM_port=9107 export CONSULT_port=9110 ##日誌 export EUREKA_log=/root/gollum/jar/log/eureka.out export ZUUL_log=/root/gollum/jar/log/zuul.out export LAB_log=/root/gollum/jar/log/laboratory.out export EXAM_log=/root/gollum/jar/log/examination.out export CONSULT_log=/root/gollum/log/consultation.out ##配置檔案,dev/prod export spring_config=prod case "$1" in start) ## 啟動EUREKA echo "--------EUREKA 開始啟動--------------" nohup java -jar $EUREKA --spring.profiles.active=$spring_config >$EUREKA_log 2>&1 & EUREKA_pid=`lsof -i:$EUREKA_port|grep "LISTEN"|awk '{print $2}'` until [ -n "$EUREKA_pid" ] do EUREKA_pid=`lsof -i:$EUREKA_port|grep "LISTEN"|awk '{print $2}'` done echo "EUREKA pid is $EUREKA_pid" echo "--------EUREKA 啟動成功--------------" ## 啟動ZUUL echo "--------開始啟動ZUUL---------------" nohup java -jar $ZUUL --spring.profiles.active=$spring_config >$ZUUL_log 2>&1 & ZUUL_pid=`lsof -i:$ZUUL_port|grep "LISTEN"|awk '{print $2}'` until [ -n "$ZUUL_pid" ] do ZUUL_pid=`lsof -i:$ZUUL_port|grep "LISTEN"|awk '{print $2}'` done echo "ZUUL pid is $ZUUL_pid" echo "---------ZUUL 啟動成功-----------" ## 啟動LAB echo "--------開始啟動LAB---------------" nohup java -jar $LAB --spring.profiles.active=$spring_config >$LAB_log 2>&1 & LAB_pid=`lsof -i:$LAB_port|grep "LISTEN"|awk '{print $2}'` until [ -n "$LAB_pid" ] do LAB_pid=`lsof -i:$LAB_port|grep "LISTEN"|awk '{print $2}'` done echo "LAB pid is $LAB_pid" echo "---------LAB 啟動成功-----------" ## 啟動EXAM echo "--------開始啟動EXAM---------------" nohup java -jar $EXAM --spring.profiles.active=$spring_config >$EXAM_log 2>&1 & EXAM_pid=`lsof -i:$EXAM_port|grep "LISTEN"|awk '{print $2}'` until [ -n "$EXAM_pid" ] do EXAM_pid=`lsof -i:$EXAM_port|grep "LISTEN"|awk '{print $2}'` done echo "EXAM pid is $EXAM_pid" echo "---------EXAM 啟動成功-----------" ## 啟動CONSULT echo "--------開始啟動CONSULT---------------" nohup java -jar $CONSULT --spring.profiles.active=$spring_config >$CONSULT_log 2>&1 & CONSULT_pid=`lsof -i:$CONSULT_port|grep "LISTEN"|awk '{print $2}'` until [ -n "$CONSULT_pid" ] do CONSULT_pid=`lsof -i:$CONSULT_port|grep "LISTEN"|awk '{print $2}'` done echo "CONSULT pid is $CONSULT_pid" echo "---------CONSULT 啟動成功-----------" echo "===startAll success===" ;; stop) P_ID=`ps -ef | grep -w $EUREKA | grep -v "grep" | awk '{print $2}'` if [ "$P_ID" == "" ]; then echo "===EUREKA process not exists or stop success" else kill -9 $P_ID echo "EUREKA killed success" fi P_ID=`ps -ef | grep -w $ZUUL | grep -v "grep" | awk '{print $2}'` if [ "$P_ID" == "" ]; then echo "===ZUUL process not exists or stop success" else kill -9 $P_ID echo "ZUUL killed success" fi P_ID=`ps -ef | grep -w $LAB | grep -v "grep" | awk '{print $2}'` if [ "$P_ID" == "" ]; then echo "===LAB process not exists or stop success" else kill -9 $P_ID echo "LAB killed success" fi P_ID=`ps -ef | grep -w $EXAM | grep -v "grep" | awk '{print $2}'` if [ "$P_ID" == "" ]; then echo "===EXAM process not exists or stop success" else kill -9 $P_ID echo "EXAM killed success" fi P_ID=`ps -ef | grep -w $CONSULT | grep -v "grep" | awk '{print $2}'` if [ "$P_ID" == "" ]; then echo "===CONSULT process not exists or stop success" else kill -9 $P_ID echo "CONSULT killed success" fi echo "===stopAll success===" ;; restart) $0 stop sleep 2 $0 start echo "===restartAll success===" ;; esac exit 0
可單檔案啟動
gullum-one.sh
#!/bin/sh #chkconfig: 2345 80 06 #description: gollum export EUREKA=/root/gollum/jar/server-eureka-1.0.jar export ZUUL=/root/gollum/jar/server-zuul-1.0.jar export LAB=/root/gollum/jar/service-laboratory-1.0.jar export EXAM=/root/gollum/jar/service-examination-1.0.jar export CONSULT=/root/gollum/jar/service-consultation-1.0.jar export EUREKA_port=8000 export ZUUL_port=8080 export LAB_port=9106 export EXAM_port=9107 export CONSULT_port=9110 export EUREKA_log=/root/gollum/jar/log/eureka.out export ZUUL_log=/root/gollum/jar/log/zuul.out export LAB_log=/root/gollum/jar/log/laboratory.out export EXAM_log=/root/gollum/jar/log/examination.out export CONSULT_log=/root/gollum/log/consultation.out ##配置檔案,dev/prod export spring_config=prod case "$1" in start) case "$2" in eureka|EUREKA) ## 啟動EUREKA echo "--------EUREKA 開始啟動--------------" nohup java -jar $EUREKA --spring.profiles.active=$spring_config >$EUREKA_log 2>&1 & EUREKA_pid=`lsof -i:$EUREKA_port|grep "LISTEN"|awk '{print $2}'` until [ -n "$EUREKA_pid" ] do EUREKA_pid=`lsof -i:$EUREKA_port|grep "LISTEN"|awk '{print $2}'` done echo "EUREKA pid is $EUREKA_pid" echo "--------EUREKA 啟動成功--------------" ;; zuul|ZUUL) ## 啟動ZUUL echo "--------開始啟動ZUUL---------------" nohup java -jar $ZUUL --spring.profiles.active=$spring_config >$ZUUL_log 2>&1 & ZUUL_pid=`lsof -i:$ZUUL_port|grep "LISTEN"|awk '{print $2}'` until [ -n "$ZUUL_pid" ] do ZUUL_pid=`lsof -i:$ZUUL_port|grep "LISTEN"|awk '{print $2}'` done echo "ZUUL pid is $ZUUL_pid" echo "---------ZUUL 啟動成功-----------" ;; lab|LAB) ## 啟動LAB echo "--------開始啟動LAB---------------" nohup java -jar $LAB --spring.profiles.active=$spring_config >$LAB_log 2>&1 & LAB_pid=`lsof -i:$LAB_port|grep "LISTEN"|awk '{print $2}'` until [ -n "$LAB_pid" ] do LAB_pid=`lsof -i:$LAB_port|grep "LISTEN"|awk '{print $2}'` done echo "LAB pid is $LAB_pid" echo "---------LAB 啟動成功-----------" ;; exam|EXAM) ## 啟動EXAM echo "--------開始啟動EXAM---------------" nohup java -jar $EXAM --spring.profiles.active=$spring_config >$EXAM_log 2>&1 & EXAM_pid=`lsof -i:$EXAM_port|grep "LISTEN"|awk '{print $2}'` until [ -n "$EXAM_pid" ] do EXAM_pid=`lsof -i:$EXAM_port|grep "LISTEN"|awk '{print $2}'` done echo "EXAM pid is $EXAM_pid" echo "---------EXAM 啟動成功-----------" ;; consult|CONSULT) ## 啟動CONSULT echo "--------開始啟動CONSULT---------------" nohup java -jar $CONSULT --spring.profiles.active=$spring_config >$CONSULT_log 2>&1 & CONSULT_pid=`lsof -i:$CONSULT_port|grep "LISTEN"|awk '{print $2}'` until [ -n "$CONSULT_pid" ] do CONSULT_pid=`lsof -i:$CONSULT_port|grep "LISTEN"|awk '{print $2}'` done echo "CONSULT pid is $CONSULT_pid" echo "---------CONSULT 啟動成功-----------" ;; esac ;; stop) case "$2" in eureka|EUREKA) P_ID=`ps -ef | grep -w $EUREKA | grep -v "grep" | awk '{print $2}'` if [ "$P_ID" == "" ]; then echo "===EUREKA process not exists or stop success" else kill -9 $P_ID echo "EUREKA killed success" fi ;; zuul|ZUUL) P_ID=`ps -ef | grep -w $ZUUL | grep -v "grep" | awk '{print $2}'` if [ "$P_ID" == "" ]; then echo "===ZUUL process not exists or stop success" else kill -9 $P_ID echo "ZUUL killed success" fi ;; lab|LAB) P_ID=`ps -ef | grep -w $LAB | grep -v "grep" | awk '{print $2}'` if [ "$P_ID" == "" ]; then echo "===LAB process not exists or stop success" else kill -9 $P_ID echo "LAB killed success" fi ;; exam|EXAM) P_ID=`ps -ef | grep -w $EXAM | grep -v "grep" | awk '{print $2}'` if [ "$P_ID" == "" ]; then echo "===EXAM process not exists or stop success" else kill -9 $P_ID echo "EXAM killed success" fi ;; consult|CONSULT) P_ID=`ps -ef | grep -w $CONSULT | grep -v "grep" | awk '{print $2}'` if [ "$P_ID" == "" ]; then echo "===CONSULT process not exists or stop success" else kill -9 $P_ID echo "CONSULT killed success" fi ;; esac ;; restart) $0 stop $2 sleep 2 $0 start $2 echo "===restart $2 success===" ;; esac exit 0
啟動命令
#全部jar執行
啟動:./gollum.sh start
停止:./gollum.sh stop
重啟:./gollum.sh restart
#單個jar執行
啟動:./gollum-one.sh start eureka 其他服務類似
停止:./gollum-one.sh stop eureka 其他服務類似
重啟:./gollum-one.sh restart eureka
開機啟動
#檔案授權: chmod +x /etc/init.d/gollum.sh chkconfig --list #指令碼新增到啟動服務中: chkconfig --add gollum.sh #指令碼啟動服務中移除: chkconfig --del gollum.sh #啟動服務: chkconfig gollum.sh on #最後reboot重啟系統即可 service gollum.sh start service gollum.sh stop
常見問題
/bin/bash^M: 壞的直譯器: 沒有那個檔案或目錄
執行shell指令碼是報錯:/bin/bash^M: 壞的直譯器: 沒有那個檔案或目錄是因為該檔案在windows系統上開啟過,關閉後其中的空格符號和Linux的不同,導致這個報錯,我們可以通過sed命令與正則的配合將檔案中的空格符號替換成linux的空格
sed -i 's/\r$//' java.sh
-bash: ./java.sh: 許可權不夠
chmod +7 java.sh