The original Lsof-i:port way to restart the Java program is often minor problems, where new scripts are improved and used in conjunction with Ansilbe, directly on scripts and samples.
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 '} '
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 "$" in
' Start ')
Start
;;
' Stop ')
Stop
;;
' Restart ')
Stop
Start
;;
' Status ')
Status
;;
' Info ')
Info
;;
*)
echo "Usage: $ {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=/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. Perform the sample:
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/87/05/wKiom1fRTW2j6HACAAFZx0BHBT0295.jpg-wh_500x0-wm_3 -wmp_4-s_3488066702.jpg "style=" Float:none; "title=" ansible1.jpg "alt=" wkiom1frtw2j6hacaafzx0bhbt0295.jpg-wh_50 "/ >
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/87/03/wKioL1fRTW2SOvT4AAEcs2vqdtQ058.jpg-wh_500x0-wm_3 -wmp_4-s_1524193302.jpg "style=" Float:none; "title=" ansible12.jpg "alt=" Wkiol1frtw2sovt4aaecs2vqdtq058.jpg-wh_50 " />
This article is from "Good Sir 2020" blog, please make sure to keep this source http://fuyuan2016.blog.51cto.com/8678344/1850858
Improved version of Java program restart, and with ansible yml batch execution