ShellLinux

常用脚本汇总

2022-02-10  本文已影响0人  子丿龙
#!/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

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
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 

上一篇下一篇

猜你喜欢

热点阅读