首页 > 代码库 > 改进版重启java 程序,及配合ansible yml 批量执行

改进版重启java 程序,及配合ansible yml 批量执行

原先的lsof -i:port 方式重启java 程序经常会有些小问题,这里改进了新脚本,并且结合ansilbe 使用,直接上脚本和样例。



  1. restat-tm.sh


#!/bin/bash



JAVA_HOME="/usr/local/java/jdk1.8.0_25" ###jdk 

APP_MAINCLASS="TM" ###TM.jar

APP_HOME="/home/java/tm" ####/home/java/tm/***

#java虚拟机启动参数

JAVA_OPTS="-ms512m -mx512m -Xmn256m -Djava.awt.headless=true -XX:MaxPermSize=128m  -Dfile.encoding=UTF-8  "




psid=0


##########################################checkpid()#################################

checkpid() {

   javaps=`$JAVA_HOME/bin/jps -l | grep $APP_MAINCLASS`

   if [ -n "$javaps" ]; then

      psid=`echo $javaps | awk ‘{print $1}‘`

   else

      psid=0  ####[ $psid -ne 0 ]

   fi



echo "checkpid:"+$psid

}





##########################################start ()#################################

start() {

   checkpid

   if [ $psid -ne 0 ]; then

      echo "================================"

      echo "warn: $APP_MAINCLASS already started! (pid=$psid)"

      echo "================================"

   else

      echo -n "Starting $APP_MAINCLASS ..."

 nohup $JAVA_HOME/bin/java $JAVA_OPTS -jar $APP_HOME/$APP_MAINCLASS.jar > /dev/null 2>&1 &

      checkpid

      if [ $psid -ne 0 ]; then

         echo "(pid=$psid) [start OK]"

      else

         echo "[start Failed]"

      fi

   fi

}



##########################################stoppid()#################################


stop() {

   checkpid

   if [ $psid -ne 0 ]; then

      echo -n "Stopping $APP_MAINCLASS ...(pid=$psid) "

      kill   $psid

      if [ $? -eq 0 ]; then

         echo "[stop OK]"

      else

         echo "[stop Failed]"

      fi


      checkpid

      if [ $psid -ne 0 ]; then

         stop   ###验证重杀

      fi

   else

      echo "================================"

      echo "warn: $APP_MAINCLASS is not running"

      echo "================================"

   fi

}



##########################################status ()#################################

status() {

   checkpid

   if [ $psid -ne 0 ];  then

      echo "$APP_MAINCLASS is running! (pid=$psid) and run time is :"

      ps -p  $psid -o pid,etime,uid,gid

   else

      echo "$APP_MAINCLASS is not running"

   fi

}


##########################################info)#################################

info() {

   echo "System Information:"

   echo "****************************"

   echo `head -n 1 /etc/issue`

   echo `uname -a`

   echo

   echo "JAVA_HOME=$JAVA_HOME"

   echo `$JAVA_HOME/bin/java -version`

   echo

   echo "APP_HOME=$APP_HOME"

   echo "APP_MAINCLASS=$APP_MAINCLASS"

   echo "****************************"

}

##########################################op ()#################################



case "$1" in

   ‘start‘)

      start

      ;;

   ‘stop‘)

     stop

     ;;

   ‘restart‘)

     stop

     start

     ;;

   ‘status‘)

     status

     ;;

   ‘info‘)

     info

     ;;

  *)

     echo "Usage: $0 {start|stop|restart|status|info}"

     exit 1

esac

exit 0






2.ansible play book 


cat jd-tm-new.yml 

---


- hosts: jd-tm-java


  sudo_user: java


  sudo: no


  tasks:


  - name: bakckup  target.tar  /home/java/war-backup 


    shell: mv  -f /home/java/target.tar  /home/java/war-backup/


    ignore_errors: True



  - name: rm  old tm/*


    shell: rm  -rf /home/java/tm/*


    ignore_errors: True



  - name:  stop tm  java 


    shell: /home/java/restart-tm.sh  stop 


    register: outMessg


  - debug: var=outMessg.stdout_lines



    ignore_errors: True



  - name: Copy war  file to client


    copy: src=http://www.mamicode.com/root/.jenkins/jobs/jd-tm/workspace/target.tar dest=/home/java


 

  - name: sleep 5


    shell:  sleep 5



 

  - name: tar xvf target.tar


    shell:  tar -xvf /home/java/target.tar


 

  - name: mv  target.tar  tm ,porject name


    shell:  mv -f  /home/java/target/*  /home/java/tm/






 


  - name: start   tm  java


    shell:  /home/java/restart-tm.sh  start 


  - shell: ps -eo pid,cmd,etime,stime | grep java

    register: ps


  - debug: var=ps.stdout_lines





3.执行样例:


技术分享


技术分享





本文出自 “好先生2020” 博客,请务必保留此出处http://fuyuan2016.blog.51cto.com/8678344/1850858

改进版重启java 程序,及配合ansible yml 批量执行