先要用shell腳本把jar複製到對應的服務器上 java
#!/bin/bash scp -r /home/git/.jenkins/workspace/x-x/x-x/target/x-x.jar tomcat@XX.XX.com:/lxm/deploy/ echo "複製完成" ssh tomcat@wapps.xingmandi.com "sh ./script/subscibe.sh" exit
#vim subscibe.shgit
#!/bin/bash #jdk環境變量 export JAVA_HOME=/usr/local/jdk1.8.0_181 export JAVA_BIN=$JAVA_HOME/bin export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH date=`date +%Y%m%d%H%M%S` APP_NAME=subscibe-wechat.jar #檢查程序是否在運行 is_exist(){ pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' ` #若是不存在返回1,存在返回0 if [ -z "${pid}" ]; then return 1 else return 0 fi } is_exist if [ $? -eq "0" ]; then kill -9 $pid else echo "${APP_NAME} is not running" fi cd /lxm/wapps mv ${APP_NAME} backup/${APP_NAME%%.*}${date}.jar echo "備份成功" cd /lxm/deploy cp -r ${APP_NAME} /lxm/wapps rm -rf ${APP_NAME} cd /lxm/wapps is_exist if [ $? -eq "0" ]; then echo "${APP_NAME} is already running. pid=${pid} ." else nohup java -jar /lxm/wapps/$APP_NAME --spring.profiles.active=prod >/lxm/wapps/subscibe.log 2>&1 & echo "程序已啓動..." fi is_exist if [ $? -eq "0" ]; then echo "${APP_NAME} is running. pid is ${pid} " else echo "${APP_NAME} is not running." fi exit