常用脚本汇总

  • java启动脚本
    命令示例: sh service.sh restart
#!/bin/bash
#
#kconfig:   - 20 80
# description: Starts and stops the App.

ENV=dev
RUNNING_USER=root
ADATE=`date +%Y%m%d%H%M%S`
APP_NAME=kids-world-api-2

APP_HOME=`pwd`
dirname $0|grep "^/" >/dev/null
if [ $? -eq 0 ];then
   APP_HOME=`dirname $0`
else
    dirname $0|grep "^\." >/dev/null
    retval=$?
    if [ $retval -eq 0 ];then
        APP_HOME=`dirname $0|sed "s#^.#$APP_HOME#"`
    else
        APP_HOME=`dirname $0|sed "s#^#$APP_HOME/#"`
    fi
fi

if [ ! -d "$APP_HOME/logs" ];then
  mkdir $APP_HOME/logs
fi

LOG_PATH=$APP_HOME/logs/$APP_NAME.out

if [ ! -f "$APP_HOME/logs/$APP_NAME.out" ];then
  touch $APP_HOME/logs/$APP_NAME.out
fi


#JMX监控需用到
#JMX="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1091 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
#JVM参数
JVM_OPTS="-Dname=$APP_NAME -Duser.timezone=GMT+08 -Xms256M -Xmx1024M"
# jar包文件名
JAR_FILE=$APP_HOME/$APP_NAME.jar
# 自定义参数
COUMSUMER_ARGS="-Dspring.profiles.active=prod -Dserver.port=8082"
pid=0
start(){
  checkpid
  if [ ! -n "$pid" ]; then
    # >$LOG_PATH 将标准输出输出到LOG_PATH文件中, 2>$1 将错误输出输出到标准输出
    JAVA_CMD="nohup java $JVM_OPTS -jar $COUMSUMER_ARGS $JAR_FILE >$LOG_PATH 2>&1 &"
    su - $RUNNING_USER -c "$JAVA_CMD"
    echo "---------------------------------"
    echo "启动完成,按CTRL+C退出日志界面即可>>>>>"
    echo "---------------------------------"
    sleep 2s
    tail -f $LOG_PATH
  else
      echo "$APP_NAME is runing PID: $pid"
  fi

}

jstart(){
  checkpid
  if [ ! -n "$pid" ]; then
    # >$LOG_PATH 将标准输出输出到LOG_PATH文件中, 2>$1 将错误输出输出到标准输出
    source /etc/profile
    JAVA_CMD="nohup java $JVM_OPTS -jar $COUMSUMER_ARGS $JAR_FILE >$LOG_PATH 2>&1 &"
    su - $RUNNING_USER -c "$JAVA_CMD"
    echo "---------------------------------"
    echo "启动完成,按CTRL+C退出日志界面即可>>>>>"
    echo "---------------------------------"
    sleep 2s
  else
      echo "$APP_NAME is runing PID: $pid"
  fi

}


status(){
   checkpid
   if [ ! -n "$pid" ]; then
     echo "$APP_NAME not runing"
   else
     echo "$APP_NAME runing PID: $pid"
   fi
}

checkpid(){
    pid=`ps -ef |grep $JAR_FILE |grep -v grep |awk '{print $2}'`
}

stop(){
    checkpid
    if [ ! -n "$pid" ]; then
     echo "$APP_NAME not runing"
    else
      echo "$APP_NAME stop..."
      kill -9 $pid
    fi
}

restart(){
    stop
    sleep 1s
    start
}

jrestart(){
    stop
    sleep 1s
    jstart
}

case $1 in
    start)  start;;
    jstart)  jstart;;
    stop)  stop;;
    restart)  restart;;
    jrestart)  jrestart;;
    status)  status;;
    *)  echo "require start|stop|restart|status"  ;;
esac

  • jenkins发布回滚脚本
    该脚本内部启动了两个java启动命令,中间间隔了60s,等待第一个jar包起来,然后判断了一下,再启动另一个jar
    命令示例:sh deploy_rollback.sh Deploy 16 14
BUILD_TYPE=$1
BUILD_NUMBER=$2
Version=$3

case $BUILD_TYPE in
  Deploy)
    echo "BUILD_TYPE:$BUILD_TYPE"
    path="bak/$BUILD_NUMBER"
    if [ -d $path ];
    then
        cp -f *.jar $path
        cp -f *.jar  /usr/local/project/kids-world/kids-world-api.jar
        cp -f *.jar  /usr/local/project/kids-world-2/kids-world-api-2.jar
    else
        echo "The files is not exists "
    fi
    echo "completing"
    ;;
  Rollback)
    echo "BUILD_TYPE:$BUILD_TYPE"
    echo "Version:$Version"
    cp -f bak/$Version/*.jar  ./
    cp -f *.jar  /usr/local/project/kids-world/kids-world-api.jar
    cp -f *.jar  /usr/local/project/kids-world-2/kids-world-api-2.jar
    ;;
  *)
  exit
      ;;
esac

rm -f kids-world-api.jar

sh /usr/local/project/kids-world/service.sh   jrestart

sleep 60s


JAR_FILE=/usr/local/project/kids-world/kids-world-api.jar

pid=`ps -ef |grep $JAR_FILE |grep -v grep |awk '{print $2}'`

if [ ! -n "$pid" ]; then
    echo "kids 8081 not runing"
else
    echo "kids 8081 is runing"
    echo "start kids 8082"
    sh /usr/local/project/kids-world-2/service.sh   jrestart
fi
  • 删除备份脚本
    命令示例:sh rv_bak.sh
ReservedNum=100  #保留文件数
FileDir=/usr/local/project/kids-prod/bak
date=$(date "+%Y%m%d-%H%M%S")

cd $FileDir   #进入备份目录
FileNum=$(ls -l | grep '^d' | wc -l)   #当前有几个文件夹,即几个备份

while(( $FileNum > $ReservedNum))
do
    OldFile=$(ls -rt | head -1)         #获取最旧的那个备份文件夹
    echo  $date "Delete File:"$OldFile
    rm -rf $FileDir/$OldFile
    let "FileNum--"
done 

你可能感兴趣的:(常用脚本汇总)