Java項目主機啓停腳本示例


啓動腳本


示例腳本以下:html

# 定義變量
JAVA_HOME=/usr/local/java/jdk1.8.0_301
LOG_HOME=/app/iacctapp/applog/rd-cbn
JVM_ARGS="-XX:-UseAdaptiveSizePolicy -XX:SurvivorRatio=8 -Xms1024m -Xmx1024m"

# 啓動項目 
nohup ${JAVA_HOME}/bin/java ${JVM_ARGS} -Dloader.path=.,lib,classes -Dacctmgr.ms.instid=funddmdbcbn -jar acctmgr-boot.jar --server.port=32210 >${LOG_HOME}/consoleDmdbFund.log 2>&1 &

# 查看日誌 
tail -f ${LOG_HOME}/consoleDmdbFund.log

定義變量
  • JAVA_HOME:jdk安裝目錄
  • LOG_HOME:程序日誌輸出目錄
  • JVM_ARGS:jvm啓動參數

啓動項目
  • nohup :不掛斷地運行命令。語法:nohup Command [ Arg … ] [ & ]
  • & :在後臺運行通常nohup 和&一塊兒用 :nohup command &
  • -Dloader.path:jvm加載目錄,有多個用逗號分隔
  • -Dacctmgr.ms.instid=funddmdbcbn:自定義,方便進程定位
  • --server.port:端口暴露
  • 2>&1:將標準錯誤輸出重定向到標準輸出(屏幕)
名稱 名稱 操做符 Java中表示
標準輸入(stdin) 0 < 或 << System.in
標準輸出(stdout) 1 >, >>, 1> 或 1>> System.out
標準錯誤輸出(stderr) 2 2> 或 2>> System.err

2>&1通常放在最後,標準錯誤輸出和程序日誌纔會都寫到日誌文件中。若是不放到最後,文件中只會輸入日誌,而標準錯誤輸出是輸出到屏幕
linux命令三劍客可參考Linux命令三劍客java


查看日誌

tail -f:動態輸出日誌文件內容到屏幕中linux


中止腳本


示例腳本以下:bash

#!/bin/bash

# 查找對應進程並殺掉
ps -ef | grep "Dacctmgr.ms.instid=fundcbn"| grep -v grep | awk '{print $2}' | while read processId 
do
        echo "stop acctmgr process $processId."
        kill $processId
done

# 顯示殺進程過程與結果 
waitTimes=0
while true
do
        let waitTimes++

        commanderNum=`ps -ef | grep "Dacctmgr.ms.instid=fundcbn"| grep -v grep | wc -l`
        if [ ${commanderNum} -eq 0 ]; then
                echo "acctmgr process has been stopped."
                break;
        else
                echo "Wait for acctmgr process to exit, waitTimes ${waitTimes} ..."
        fi
        
        sleep 1;
done

查找對應進程

ps -ef:用標準的格式顯示進程app

grep "Dacctmgr.ms.instid=fundcbn":控制ps命令只輸出Dacctmgr.ms.instid=fundcbn相關的進程jvm

grep -v grep:過濾掉grep自己進程日誌

awk '{print $2}':每行以空格或分隔符切割,輸出第3位(從0計起)code


殺進程
while read processId 
do
        echo "stop acctmgr process $processId."
        kill $processId
done

將上面讀到的進程殺掉,知道全部進程中止爲止server


顯示殺進程過程與結果
while true
do
        let waitTimes++

        commanderNum=`ps -ef | grep "Dacctmgr.ms.instid=fundcbn"| grep -v grep | wc -l`
        if [ ${commanderNum} -eq 0 ]; then
                echo "acctmgr process has been stopped."
                break;
        else
                echo "Wait for acctmgr process to exit, waitTimes ${waitTimes} ..."
        fi
        
        sleep 1;
done

該段代碼的做用是每隔一秒循環對應進程的掃描,若是的進程還存在對應進程,輸出提示語句,直到進程不存在爲止htm

相關文章
相關標籤/搜索