Linux

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > Linux > shell脚本启动springboot项目

shell脚本如何启动springboot项目

作者:yuanteng101

这篇文章主要介绍了shell脚本如何启动springboot项目问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

shell脚本启动springboot项目

Spring Boot项目启动

我们知道打包好的Spring Boot项目这样启动

java -jar -Dfile.encoding=utf-8 test-0.0.1-SNAPSHOT.jar

在linux中,常常使用nohup后台启动其他程序,Spring Boot项目使用nohup启动如下

nohup java -jar -Dfile.encoding=utf-8 test-0.0.1-SNAPSHOT.jar &

如果使用上面命令启动,注意这时候远程terminal要主动exit退出,不这样做系统可能会杀掉上面启动的进程

由于开发需要,需要频繁地更新项目,写了两个shell脚本启动和关闭项目

#!/bin/bash
cd project  # ccd2002-0.0.1-SNAPSHOT.jar所在目录
nohup java -Xms128m -Xmx256m -jar -Dfile.encoding=utf-8 ccd2002-0.0.1-SNAPSHOT.jar &
#!/bin/bash
sudo kill $(ps -aux | grep 'ccd2002' | awk 'NR==2{print $2}')

上面脚本使用ps命令查到ccd2002项目的信息,然后使用awk过滤信息获取项目pid,最后使用kill杀掉项目。

为了方便大家理解,下面分解

sudo kill $(ps -aux | grep 'ccd2002' | awk 'NR==2{print $2}')
ps -aux | grep 'ccd2002'

命令执行结果截图如下

ps -aux | grep 'test' | awk 'NR==2{print $2}'

命令取出上图中指出的pid

awk命令对ps结果进行处理,awk 'NR==2{print $2}'中的NR=2表示ps结果的第二行,$2表示行中的第二个元素

springboot项目linux简单的启动、停止、更新脚本sh

1.启动脚本

#!/bin/bash
#获取路径
basepath=$(cd $(dirname $0); pwd)
echo  "xxxxxx  启动开始............."
nohup java -jar  $basepath/xxxxxx.jar  --spring.profiles.active=prd >/dev/null 2>&1&
echo  "xxxxxx  启动成功............."

2.停止脚本

#!/bin/bash
echo "xxxxxx    停止开始......."
ID=`ps -ef | grep "xxxxxx.jar" | grep -v "grep" | awk '{print $2}'`
echo $ID
for id in $ID
do
kill -9 $id
echo "killed $id"
done
sleep 5s
echo "xxxxxx    停止成功......."

3.版本升级脚本

#!/bin/bash
basepath=$(cd $(dirname $0); pwd)
echo "############################执行update.sh开始###########################"
echo "基础路径:$basepath"
echo "远程传递参数为:" ${1}
if [ -z ${1} ];then 
echo "工程名称参数传递为空,更新失败"
else
if [ -f "$basepath/../../deploy/${1}.jar" ];then
        echo "开始备份工程包"
        cp $basepath/${1}.jar  $basepath/../appBak/${1}.jar_$(date '+%Y-%m-%d-%H:%M:%S')
        echo "工程包备份成功"
        #停止应用    
        sh $basepath/stop.sh
        #开始删除老的工程包
        if [ -f "$basepath/${1}.jar" ];then
        rm $basepath/${1}.jar
        fi
        echo "开始更新工程包"
     mv $basepath/../../deploy/${1}.jar $basepath/${1}.jar
       if [ $? -eq 0 ];then
        #启动应用
        sh $basepath/start.sh
        else
          echo "工程包移动失败"
        fi
else
 echo "升级包不存在"
fi
fi
echo "############################执行update.sh结束###########################"

4、回滚脚本

#!/bin/bash
basepath=$(cd $(dirname $0); pwd)
echo "基础路径:$basepath"
packageName=$(cd ${basepath}'/../appBak'; ls -lrlt |grep $(date '+%Y-%m-%d') |head -n 1 |awk '{print $9}' )
echo "获取回滚包名称为:"$packageName
if [ -z $packageName ];then
   echo "回滚 "$1" 包不存在,回滚失败"
else
echo "远程传递参数为:"${1}
if [ -z ${1} ];then
echo "工程名称参数为空,回滚失败"
else
#将备份包还原在启动
cp $basepath/../appBak/$packageName $basepath/../../deploy/${1}.jar
if [ -f "$basepath/../../deploy/"${1}".jar" ];then
        #停止应用    
        sh $basepath/stop.sh
        if [ -f "$basepath/"${1}".jar" ];then
        #开始删除老的工程包
        rm $basepath/${1}.jar
        fi
        mv $basepath/../../deploy/${1}.jar $basepath/${1}.jar
        if [ $? -eq 0 ];then
         echo "回滚还原包成功"
        #启动应用
        sh $basepath/start.sh
        else
          echo  "工程包还原失败"
        fi
else
 echo "还原包不存在"
fi
fi
fi

5.远程发布脚本(前提配置免密登录)

#!/bin/bash
echo "******************************远程发布脚本***********************************************************"
echo " "
echo "********脚本参数 工程名称:支持 ddp-server、ddp-server-idxs、ddp-server-mdxs、ddp-server-ext、********"
echo "********ddp-server-task、dmsp-gateway 脚本类型:支持 update(更新脚本)/rollbak(回滚脚本)***************"
echo "*******将上述工程名对应的jar包放在 /opt/apps/deploy目录下********************************************"
echo "********在config.properties配置文件中,配置工程对应的服务器IP多台机器则以逗号(,)分隔。***************"
echo "********如 ddp-server=192.168.1.2,192.178.3.4 的形式配置工程对应的机器*******************************"
echo "*************执行脚本的方式如  sh deploy.sh ddp-server update  升级**********************************"
echo "*************执行脚本的方式如  sh deploy.sh ddp-server rollbak 回滚**********************************"
echo "  "
echo "******************************远程发布脚本***********************************************************"
echo "     "
echo "##############################开始校检参数############################################################"
if [ -z $1 ]||[ -z $2 ];then
echo " "
echo '参数不能为空,如: sh deploy.sh ddp-server update'
echo ""
echo "##############################参数校检失败############################################################"
else
echo " "
echo "##############################参数校检成功############################################################"
echo "    "
echo "#############################开始校检配置文件#########################################################"
echo "  "
path="/opt/apps/deploy"
echo "远程发布包的路径为:" $path
shellpath=""
if [ $1 == "ddp-server" ];then
 shellpath="/opt/apps/ddp/app"
elif [ $1 == "ddp-server-idxs" ];then
 shellpath="/opt/apps/ddp-idxs/app"
elif [ $1 == "ddp-server-mdxs" ]; then
 shellpath="/opt/apps/ddp-mdxs/app"
elif [ $1 == "ddp-server-ext" ]; then
  shellpath="/opt/apps/ddp-ext/app"
elif [ $1 == "ddp-server-task" ];then
  shellpath="/opt/apps/ddp-task/app"
elif [ $1 == "dmsp-gateway" ];then
  shellpath="/opt/apps/dmsp-gateway/app"
fi 
echo "远程脚本路径为:"$shellpath
#工程模块名称
modle=$(cat config.properties |grep  $1"=" |awk -F '=' '{print $1}')
echo "工程名称为:"$modle
ips=$(cat config.properties |grep  $1"="|awk -F '=' '{print $2}')
echo 目标ip为:$ips
echo " "
if [ -z $modle ];then
echo "###########工程在配置文件(config.properties)中不存在,脚本执行终止#####################################"
elif [ -z $ips ]; then
echo "#############工程对应的目标机器ip在配置文件(config.properties)中不存在,脚本执行终止##################"
elif [ -z $shellpath ];then
echo "############################"$1"不支持远程操作,脚本执行终止##########################################"
elif [ $2 != 'update' ] && [ $2 != 'rollbak' ];then
echo "############################远程操作仅支持 update 或 rollbak,脚本执行终止############################"
else
echo "###################配置文件校检通过,开始更新或回滚操作###############################################"
OLD_IFS="$IFS"
IFS=","
#如下会自动分隔
arr=($ips)
#恢复原来的分隔符
IFS="$OLD_IFS"
#遍历数组
for ip in ${arr[@]}
do
 if [ $2 == 'update' ];then
  echo "     "
  echo "====================================================================================================="
  echo "++++++++++++++++++++++++++++++++++++"${ip}"开始执行更新操作++++++++++++++++++++++++++++++++"
  echo "     "
  echo "开始远程拷贝文件" ${modle}".jar 到机器ip为: "${ip} ${path}"的目录下"
   scp  -o ConnectTimeout=10 ${path}/${modle}.jar phfund@${ip}:${path}
  if [ $? -ne 0 ];then
      echo "远程拷贝"${modle}".jar 到 " ${ip} " 失败,本次更新失败"
      echo "  "
     echo "======================================================================================================"
  else
     echo "远程拷贝"  ${modle}".jar到" $ip "成功"
      ssh  -o ConnectTimeout=10  ${ip}   ' sh ' $shellpath'/'$2'.sh ' $1 
      if [ $? -eq 0 ];then
      echo "     "
      echo "+++++++++++++++++++++++++++++++++++++++++++++"${ip}"更新成功+++++++++++++++++++++++++++"
      echo "================================================================================================="
      else
       echo "     "
       echo "+++++++++++++++++++++++++++++++++++++++++++++"${ip}"更新失败++++++++++++++++++++++++++"   
       echo "================================================================================================"
      fi
   fi
elif [ $2=='rollbak' ];then
      echo "     "
     echo "==================================================================================================="   
     echo "+++++++++++++++++++++++++++++++++++++++++++++++++++"${ip}"开始执行回滚操作++++++++++++++"
     echo "     "
     ssh  -o ConnectTimeout=10 ${ip}   ' sh '${shellpath}'/'$2'.sh ' $1 
      if [ $? -ne 0 ];then
        echo "     "
        echo "+++++++++++++++++++++++++++++++++++++++++++++++"${ip}"回滚操作失败++++++++++++++++++++"    
         echo "===============================================================================================" 
     else
        echo "     "
        echo "+++++++++++++++++++++++++++++++++++++++++++++"${ip}"回滚操作成功++++++++++++++++++++++"   
        echo "==============================================================================================="
        fi
fi
done
fi
fi
#+++++++++++++++++脚本说明++++++++++++++++++++++++++++++
echo "******************************远程发布脚本说明***********************************************************************"
echo " "
echo "******************************脚本参数*******************************************************************************"
echo "**参数1 工程名称:支持 ddp-server、ddp-server-idxs、ddp-server-mdxs、ddp-server-extddp-server-task、dmsp-gateway ...**"
echo "**参数2 配置属性中对应工程的标识:支持 1,2,3...,当为 all 的时候取全部[1,2,3...]所对应的属性**************************"
echo "**参数3 执行的的脚本:支持 update(版本升级),rollbak(版本回滚),log(查看版本日志),start(启动),stop(停止),restart(重启)**"
echo "******************************脚本参数*******************************************************************************"
echo " "
echo "******************************升级版本的工程包放在 /opt/apps/deploy目录下********************************************"
echo "******************************在/opt/apps/ssh/config.properties配置文件中配置属性************************************"
echo "******************************工程ip如: ddp-server.1=ip**************************************************************"
echo "******************************工程日志路径如: ddp-server.logPath=/opt/apps/ddp/log/**********************************"
echo "******************************工程执行脚本路径如: ddp-server.shellPath=/opt/apps/ddp/app/****************************"
echo "  "
echo "*******************************执行方式(deploy.sh 参数1 参数2 参数3)*************************************************"
echo "*************执行脚本的方式如  deploy.sh ddp-server all update  升级所有的工程***************************************"
echo "*************执行脚本的方式如  deploy.sh ddp-server all rollbak 回滚所有的工程***************************************"
echo "*************执行脚本的方式如  deploy.sh ddp-server all stop    停止所有的工程***************************************"
echo "*************执行脚本的方式如  deploy.sh ddp-server all start   启动所有的工程***************************************"
echo "*************执行脚本的方式如  deploy.sh ddp-server all restart 重启所有的工程***************************************"
echo " "
echo "*************日志查看不支持all,只能够单独选择某一台日志查看**********************************************************"
echo "**************执行脚本的方式如 deploy.sh ddp-server 1 log 查看某台机器的工程日志*************************************"
echo "  "
echo "******************************远程发布脚本说明***********************************************************************"
echo "  "
#+++++++++++++++++脚本说明++++++++++++++++++++++++++++++
#+++++++++++++++++参数定义部分++++++++++++++++++++++++++
#参数一表示 工程名称
param1=$1
#参数二表示 配置文件中对应的属性,规则 1,2,3 分别取对应的参数,当为 all的时候取全部[1,2,3]的属性
param2=$2
#参数三表示 执行的的脚本支持update(版本升级),rollbak(版本回滚),log(查看版本日志),start(启动),stop(停止),restart(重启)
param3=$3
#工程包名称
packageName=$param1".jar"
#升级包路径
updatepath="/opt/apps/deploy"
#远程机器用户
userName="phfund"
#ip地址
IP=""
#日志路径
logPath=""
#脚本路径
shellPath=""
#config.properties配置文件路径
configPath="/opt/apps/ssh"
#+++++++++++++++++参数定义部分++++++++++++++++++++++++++
#+++++++++++++++++传递参数校检以及配置文件解析校检部分++++++++++
echo "**************************************开始校检传入参数*******************************************************************"
if [ -z $param1 ]||[ -z $param2 ]||[ -z $param3 ] ;then
echo " " 
echo "参数不能为空如:(deploy.sh 参数1 参数2 参数3) 按照空格分隔"
echo " "
echo "**************************************传入参数校检失败*******************************************************************"
exit
fi
if [ "$param3" != "update" ]&&[ "$param3" != "restart" ]&&[ "$param3" != "stop" ]&&[ "$param3" != "start" ]&&[ "$param3" != "rollbak" ]&&[ "$param3" != "log" ];then
echo " " 
echo "参数3只支持: update,restart,start,stop,rollbak,log"
echo " "
echo "**************************************传入参数校检失败*******************************************************************"
exit
else
   if [ "$param3" == "log" ]&&[ "$param2" == "all" ];then
echo " " 
echo "查看系统日志只支持单个操作如:(deploy.sh ddp-server 1 log),参数2不支持all"
echo " "
echo "**************************************传入参数校检失败*******************************************************************"   
    exit
   fi
fi
echo " "
echo "**************************************传入参数校检成功*******************************************************************"
echo " "
echo "*****************************************开始解析config.properties配置文件***********************************************" 
echo " "
if [ "$param2" == "all" ];then
  IP=$(cd ${configPath}; cat ./config.properties|grep ${param1}'\.'|grep -v ${param1}'\.logPath'|grep -v ${param1}'\.shellPath'|awk -F '=' '{print $2}')
else
  IP=$(cd ${configPath};cat ./config.properties|grep ${param1}'\.'${param2}|awk -F '=' '{print $2}') 
fi
echo "获取工程名称为: "$param1 " IP地址: "$IP
 logPath=$(cd ${configPath}; cat ./config.properties|grep  ${param1}'\.logPath'|awk -F '=' '{print $2}')
echo "获取工程日志路径为: "$logPath 
 shellPath=$(cd ${configPath}; cat ./config.properties|grep  ${param1}'\.shellPath'|awk -F '=' '{print $2}')
echo "获取工程执行脚本路径为: "$shellPath
echo " "
echo "**************************************解析config.properties配置文件结束***********************************************"
#+++++++++++++++++传递参数校检以及配置文件解析部分++++++++++
#+++++++++++++++++解析参数验证,以及执行调度部分++++++++++++
echo " "
echo "**************************************开始校检解析参数*****************************************************************"
if [ -z $IP ];then
echo ""
echo "获取工程的IP为空,终止程序"
echo " "
echo "**************************************解析参数校检失败*****************************************************************"
exit
fi
if [ -z $shellPath ];then
echo ""
echo "获取工程的执行脚本路径为空,终止程序"
echo " "
echo "**************************************解析参数校检失败*****************************************************************"
exit
fi
if [ "$param3" == "log" ]&&[ -z $logPath ];then
echo ""
echo "查看日志,获取工程的日志路径为空,终止程序"
echo " "
echo "**************************************解析参数校检失败*****************************************************************"
exit
fi
echo " "
echo "**************************************解析参数校检成功*****************************************************************"
#调度公共方法
function remotecall(){  
    ssh  -o ConnectTimeout=10  ${ip}   'sh ' $shellPath'/'$param3'.sh ' $param1 
}  
#查看日志方法
function looklog(){
   ssh  -o ConnectTimeout=10  ${ip}   'sh ' $shellPath'/'$param3'.sh ' $param1 $logPath
}
#远程拷贝公共方法
function scpcall(){
    scp  -o ConnectTimeout=10 ${updatepath}/${packageName} ${userName}@${ip}:${updatepath}
}
for ip in $IP
do
if [ $param3 == 'update' ];then
echo " "
echo "====================================升级(update)操作执行开始==========================================================="
echo " "
   scpcall
 if [ $? -ne 0 ];then
   echo "远程拷贝"${packageName}" 到IP为" ${ip} "机器的 "${updatepath}" 目录下失败,本次升级失败" 
 else
   echo "远程拷贝"${packageName}" 到IP为" $ip "机器的 "${updatepath}" 目录下成功,开始执行更新脚本"
   echo " "
   remotecall
   echo " "
   if [ $? -ne 0 ];then
     echo "机器IP为 "$ip "执行更新脚本失败"
   else
     echo "机器IP为 "$ip "执行更新脚本成功"
   fi 
 fi
echo " "
echo "====================================升级(update)操作执行结束==========================================================="
elif [ $param3 == 'start' ];then
echo " "
echo "====================================启动(start)操作执行开始============================================================"
echo " "
remotecall
 if [ $? -ne 0 ];then
     echo "机器IP为 "$ip "执行启动脚本失败"
   else
     echo "机器IP为 "$ip "执行启动脚本成功"
   fi
echo " "
echo "====================================启动(start)操作执行结束============================================================"
elif [ $param3 == 'rollbak' ];then
echo " "
echo "===================================回滚(rollbak)操作执行开始============================================================"
echo " "
remotecall
 if [ $? -ne 0 ];then
     echo "机器IP为 "$ip "执行回滚脚本失败"
   else
     echo "机器IP为 "$ip "执行回滚脚本成功"
   fi
echo " "
echo "====================================回滚(rollbak)操作执行结束============================================================"
elif [ $param3 == 'stop' ];then
echo " "
echo "===================================停止(stop)操作执行开始============================================================"
echo " "
remotecall
 if [ $? -ne 0 ];then
     echo "机器IP为 "$ip "执行停止脚本失败"
   else
     echo "机器IP为 "$ip "执行停止脚本成功"
   fi
echo " "
echo "====================================停止(stop)操作执行结束============================================================"
elif [ $param3 == 'log' ];then
echo " "
echo "===================================查看日志(log)操作执行开始============================================================"
echo " "
 looklog
 if [ $? -ne 0 ];then
     echo "机器IP为 "$ip "执行日志查看脚本失败"
   else
     echo "机器IP为 "$ip "执行日志查看脚本成功"
   fi
echo " "
echo "====================================查看日志(log)操作执行结束============================================================"
elif [ $param3 == 'restart' ];then
echo " "
echo "======================================重启(restart)操作执行开始=========================================================="
echo " "
remotecall
 if [ $? -ne 0 ];then
     echo "机器IP为 "$ip "执行重启脚本失败"
   else
     echo "机器IP为 "$ip "执行重启脚本成功"
   fi
echo " "
echo "=====================================重启(restart)操作执行结束============================================================"
fi
done
#+++++++++++++++++解析参数验证,以及执行调度部分++++++++++++

优化版本入口

#!/bin/bash
#+++++++++++++++++脚本说明++++++++++++++++++++++++++++++
help=$1
if [ "${help^^}" == "-HELP" ];then
echo "******************************远程发布脚本说明***********************************************************************"
echo " "
echo "******************************脚本参数*******************************************************************************"
echo "**参数1 工程名称:支持 ddp-server、ddp-server-idxs、ddp-server-mdxs、ddp-server-extddp-server-task、dmsp-gateway ...**"
echo "**参数2 执行的的脚本:支持 update(版本升级),rollbak(版本回滚),log(查看版本日志),start(启动),stop(停止),restart(重启)**"
echo "**参数3 配置属性中对应工程机器的标识:支持 ddp-server1,ddp-server2,ddp-server3...,当为 all 的时候取全部[ddp-server1,*"
echo "***************ddp-server2,ddp-server3.]所对应的属性*****************************************************************"
echo "******************************脚本参数*******************************************************************************"
echo " "
echo "******************************升级版本的工程包放在 /opt/apps/deploy目录下********************************************"
echo "******************************在/opt/apps/ssh/config.properties配置文件中配置属性************************************"
echo "******************************工程ip如: ddp-server.1=ip**************************************************************"
echo "******************************工程日志路径如: ddp-server.logPath=/opt/apps/ddp/log/**********************************"
echo "******************************工程执行脚本路径如: ddp-server.shellPath=/opt/apps/ddp/app/****************************"
echo "  "
echo "*******************************执行方式(deploy.sh 参数1 参数2 参数3)*************************************************"
echo "*************执行脚本的方式如  deploy.sh ddp-server  update all 升级所有的工程***************************************"
echo "*************执行脚本的方式如  deploy.sh ddp-server  rollbak all回滚所有的工程***************************************"
echo "*************执行脚本的方式如  deploy.sh ddp-server  stop all   停止所有的工程***************************************"
echo "*************执行脚本的方式如  deploy.sh ddp-server  start all  启动所有的工程***************************************"
echo "*************执行脚本的方式如  deploy.sh ddp-server restart all 重启所有的工程***************************************"
echo " "
echo "*************日志查看不支持all,只能够单独选择某一台日志查看**********************************************************"
echo "**************执行脚本的方式如 deploy.sh ddp-server log ddp-server1 查看某台机器的工程日志***************************"
echo " "
echo "*************脚本对应2个参数的语法,只支持以下语法查询 参数3 支持的个数**********************************************"
echo "**************查询某个工程对应的机器的标识如 deploy.sh 参数1 listserver 查看对应的机器参数个数***********************"
echo "  "
echo "******************************远程发布脚本说明***********************************************************************"
echo "  "
exit
fi
#+++++++++++++++++脚本说明++++++++++++++++++++++++++++++
#+++++++++++++++++参数定义部分++++++++++++++++++++++++++
#参数一表示 工程名称
param1=$1
#参数二表示 配置文件中对应的属性,规则 1,2,3 分别取对应的参数,当为 all的时候取全部[1,2,3]的属性
param2=$2
#参数三表示 执行的的脚本支持update(版本升级),rollbak(版本回滚),log(查看版本日志),start(启动),stop(停止),restart(重启)
param3=$3
#工程包名称
packageName=$param1".jar"
#升级包路径
updatepath="/opt/apps/deploy"
#远程机器用户
userName="phfund"
#ip地址
IP=""
#日志路径
logPath=""
#脚本路径
shellPath=""
#config.properties配置文件路径
configPath="/opt/apps/ssh"
#+++++++++++++++++参数定义部分++++++++++++++++++++++++++
#+++++++++++++++++传递参数校检以及配置文件解析校检部分++++++++++
if [ -z $param3 ]&&[ -n $param1 ] && [ -n $param2 ];then
 if [ "${param2^^}" == "LISTSERVER" ];then
  echo "**************************************获取工程所部署的机器(执行脚本参数3 可选的值)**************************************"
   list=$(cd ${configPath}; cat ./config.properties|grep "${param1}"[0-9]|awk -F '=' '{print $1}')
   echo "工程对应机器名称(参数3)可选值:" $list
  echo ""
  echo "**************************************获取工程所部署的机器(执行脚本参数3 可选的值)**************************************"
  exit
 fi
fi
echo "**************************************开始校检传入参数*******************************************************************"
if [ -z $param1 ]||[ -z $param2 ]||[ -z $param3 ] ;then
echo " " 
echo "参数不能为空如:(deploy.sh 参数1 参数2 参数3) 按照空格分隔"
echo " "
echo "**************************************传入参数校检失败*******************************************************************"
exit
fi
if [ "$param2" != "update" ]&&[ "$param2" != "restart" ]&&[ "$param2" != "stop" ]&&[ "$param2" != "start" ]&&[ "$param2" != "rollbak" ]&&[ "$param2" != "log" ];then
echo " " 
echo "参数2只支持: update,restart,start,stop,rollbak,log"
echo " "
echo "**************************************传入参数校检失败*******************************************************************"
exit
else
   if [ "$param2" == "log" ]&&[ "$param3" == "all" ];then
echo " " 
echo "查看系统日志只支持单个操作如:(deploy.sh ddp-server log ddp-server1),参数3不支持all"
echo " "
echo "**************************************传入参数校检失败*******************************************************************"   
    exit
   fi
fi
echo " "
echo "**************************************传入参数校检成功*******************************************************************"
echo " "
echo "*****************************************开始解析config.properties配置文件***********************************************" 
echo " "
if [ "$param3" == "all" ];then
  IP=$(cd ${configPath}; cat ./config.properties|grep "${param1}"[0-9]|awk -F '=' '{print $2}')
else
  IP=$(cd ${configPath};cat ./config.properties|grep ${param3}"="|awk -F '=' '{print $2}') 
fi
echo "获取工程名称为: "$param1 " IP地址: "$IP
 logPath=$(cd ${configPath}; cat ./config.properties|grep  ${param1}'\.logPath'|awk -F '=' '{print $2}')
echo "获取工程日志路径为: "$logPath 
 shellPath=$(cd ${configPath}; cat ./config.properties|grep  ${param1}'\.shellPath'|awk -F '=' '{print $2}')
echo "获取工程执行脚本路径为: "$shellPath
echo " "
echo "**************************************解析config.properties配置文件结束***********************************************"
#+++++++++++++++++传递参数校检以及配置文件解析部分++++++++++
#+++++++++++++++++解析参数验证,以及执行调度部分++++++++++++
echo " "
echo "**************************************开始校检解析参数*****************************************************************"
if [ -z "$IP" ];then
echo ""
echo "获取工程的IP为空,终止程序"
echo " "
echo "**************************************解析参数校检失败*****************************************************************"
exit
fi
if [ -z $shellPath ];then
echo ""
echo "获取工程的执行脚本路径为空,终止程序"
echo " "
echo "**************************************解析参数校检失败*****************************************************************"
exit
fi
if [ "$param2" == "log" ]&&[ -z $logPath ];then
echo ""
echo "查看日志,获取工程的日志路径为空,终止程序"
echo " "
echo "**************************************解析参数校检失败*****************************************************************"
exit
fi
echo " "
echo "**************************************解析参数校检成功*****************************************************************"
#将变量传递到子脚本
export updatepath
export packageName
export userName
export shellPath
export param2
export param1
export logPath
for ip in $IP
do
export ip
if [ $param2 == 'update' ];then
#执行更新脚本
sh update.sh
elif [ $param2 == 'start' ];then
#执行启动脚本
sh start.sh
elif [ $param2 == 'rollbak' ];then
#执行回滚脚本
sh rollbak.sh
elif [ $param2 == 'stop' ];then
#执行停止脚本
sh stop.sh
elif [ $param2 == 'log' ];then
#执行日志查看
sh log.sh
elif [ $param2 == 'restart' ];then
#执行重启脚本
sh restart.sh
fi
done
#+++++++++++++++++解析参数验证,以及执行调度部分++++++++++++

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文