示例腳本以下: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
nohup Command [ Arg … ] [ & ]
nohup command &
名稱 | 名稱 | 操做符 | 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