在linux寫一個shell腳本用maven git自動更新代碼並且打包部署


服務器上必須安裝了git maven jdk 並且配置好環境變量

實際服務器中可能運行着多個Java進程,所以重新部署的時候需要先停止原來的java進程,寫一個按照名稱殺死進程的腳本

kill.sh

  1. #!/bin/sh
  2. function PidFind()
  3.  
  4. {
  5.  
  6. PIDCOUNT=`ps -ef | grep $1 | grep -v "grep" | grep -v $0 | awk '{print $2}' | wc -l`;
  7.  
  8. if [ ${PIDCOUNT} -gt 1 ] ; then
  9.  
  10. echo "There are too many process contains name[$1]"
  11.  
  12. elif [ ${PIDCOUNT} -le 0 ] ; then
  13.  
  14. echo "No such process[$1]!"
  15.  
  16. else
  17.  
  18. PID=`ps -ef | grep $1 | grep -v "grep" | grep -v ".sh" | awk '{print $2}'` ;
  19.  
  20. echo "Find the PID of this progress!--- process:$1 PID=[${PID}] ";
  21.  
  22. echo "Kill the process $1 ...";
  23.  
  24. kill -9 ${PID};
  25.  
  26. echo "kill -9 ${PID} $1 done!";
  27.  
  28. fi
  29.  
  30. }
  31.  
  32.  
  33. PidFind $1
  34.  
  35. exit 1

接着就是寫重新部署的腳本

redeploy.sh

  1. #殺死原來的java進程
  2. ./kill.sh test.jar
  3. #進入代碼文件夾,必須有git管理
  4. cd code/test/
  5. #更新代碼
  6. git pull
  7. #清理原來的jar包重新打包
  8. mvn clean install -Dmaven.test.skip= true
  9. cd ~
  10. #刪除原來的jar包
  11. rm -rf test-web.jar
  12. cp code/ test/test-web/target/test-web.jar test-web.jar
  13. #后台運行
  14. nohup java -agentlib:jdwp=transport=dt_socket,address=8100,server=y, suspend=n -jar test-web.jar > /root/logs/test.log &
  15. #監控日志
  16. tail -f /root/logs/flm-material.log

上述的代碼路徑 和jar包存放位置根據實際情況修改


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM