1.shell脚本传参

https://blog.csdn.net/sinat_36521655/article/details/79296181

2.shell脚本获得其他命令的执行结果

http://book.51cto.com/art/201411/457601.htm

3.shell脚本读取输入

https://blog.csdn.net/wdz306ling/article/details/79557931

4.shell怎么修改环境变量

 

java通用启动脚本

#!/bin/bash

#程序住目录
APP_HOME=/usr/local/ods/app/dispatch

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

#需要启动的Java主程序(main方法类)
APP_MAINCLASS=com.pa.agentbank.drs.job.Start

#java虚拟机启动参数
JAVA_OPTS="-Xms1024m -Xmx1024m -Xmn256m  -Dsys_config_path=/usr/local/ods/app/dispatch/"

#拼凑完整的classpath参数,包括指定lib目录下所有的jar
for i in "$APP_HOME"/lib/*.jar; do
   CLASSPATH="$CLASSPATH":"$i"
done

for i in "$APP_HOME"/*.jar; do
   CLASSPATH="$CLASSPATH":"$i"
done

###################################
#(函数)判断程序是否已启动
#
#说明:
#使用JDK自带的JPS命令及grep命令组合,准确查找pid
#jps 加 l 参数,表示显示java的完整包路径
#使用awk,分割出pid ($1部分),及Java程序名称($2部分)
###################################
#初始化psid变量(全局)
psid=0
 
checkpid() {
   javaps=`$JAVA_HOME/bin/jps -l | grep $APP_MAINCLASS`
 
   if [ -n "$javaps" ]; then
      psid=`echo $javaps | awk '{print $1}'`
   else
      psid=0
   fi
}


###################################
#(函数)启动程序
#
#说明:
#1. 首先调用checkpid函数,刷新$psid全局变量
#2. 如果程序已经启动($psid不等于0),则提示程序已启动
#3. 如果程序没有被启动,则执行启动命令行
#4. 启动命令执行后,再次调用checkpid函数
#5. 如果步骤4的结果能够确认程序的pid,则打印[OK],否则打印[Failed]
#注意:echo -n 表示打印字符后,不换行
#注意: "nohup 某命令 >/dev/null 2>&1 &" 的用法
###################################
start() {
   checkpid
 
   if [ $psid -ne 0 ]; then
      echo "================================"
      echo "warn: $APP_MAINCLASS already started! (pid=$psid)"
      echo "================================"
   else
      echo -n "Starting $APP_MAINCLASS ..."
      #JAVA_CMD="nohup $JAVA_HOME/bin/java $JAVA_OPTS -classpath $CLASSPATH $APP_MAINCLASS >/dev/null 2>&1 &"
      #su - $RUNNING_USER -c "$JAVA_CMD"
      #nohup java -cp $CLASSPATH $APP_MAINCLASS >/dev/null 2>&1 &
      #nohup java $JAVA_OPTS -cp  ./app_conf:$CLASSPATH $APP_MAINCLASS >/usr/local/ods/app/process/log4j/shellStart.log &
      nohup java -cp  ./app_conf:$CLASSPATH $APP_MAINCLASS >/dev/null 2>&1 &
      sleep 3
      checkpid
      if [ $psid -ne 0 ]; then
         echo "(pid=$psid) [OK]"
      else
         echo "[Failed]"
      fi
   fi
}

###################################
#(函数)停止程序
#
#说明:
#1. 首先调用checkpid函数,刷新$psid全局变量
#2. 如果程序已经启动($psid不等于0),则开始执行停止,否则,提示程序未运行
#3. 使用kill -9 pid命令进行强制杀死进程
#4. 执行kill命令行紧接其后,马上查看上一句命令的返回值: $?
#5. 如果步骤4的结果$?等于0,则打印[OK],否则打印[Failed]
#6. 为了防止java程序被启动多次,这里增加反复检查进程,反复杀死的处理(递归调用stop)。
#注意:echo -n 表示打印字符后,不换行
#注意: 在shell编程中,"$?" 表示上一句命令或者一个函数的返回值
###################################
stop() {
   checkpid
 
   if [ $psid -ne 0 ]; then
      echo -n "Stopping $APP_MAINCLASS ...(pid=$psid) "
      #su - $RUNNING_USER -c "kill -9 $psid"
      kill -9 $psid
      if [ $? -eq 0 ]; then
         echo "[OK]"
      else
         echo "[Failed]"
      fi
             sleep 3
      checkpid
      if [ $psid -ne 0 ]; then
         stop
      fi
   else
      echo "================================"
      echo "warn: $APP_MAINCLASS is not running"
      echo "================================"
   fi
}

###################################
#(函数)检查程序运行状态
#
#说明:
#1. 首先调用checkpid函数,刷新$psid全局变量
#2. 如果程序已经启动($psid不等于0),则提示正在运行并表示出pid
#3. 否则,提示程序未运行
###################################
status() {
   checkpid
 
   if [ $psid -ne 0 ];  then
      echo "$APP_MAINCLASS is running! (pid=$psid)"
   else
      echo "$APP_MAINCLASS is not running"
   fi
}
 

case "$1" in
   'start')
      start
      ;;
   'stop')
     stop
     ;;   
   'status')
     status
     ;;   
  *)
     echo "Usage: $0 {start|stop|status}"
     ;;
   esac

 jenkins脚本

#!/bin/sh
. /etc/profile
#输入参数1 项目名称,也是tar包的名称
app_name=$1
if [ 0 == ${app_name:-0} ]
then 
    echo "please input the app name"
    exit 1
fi
tomcat_home=/usr/local/tomcat
tomcat_webapp=$tomcat_home/webapps/$app_name
SHUTDOWN=$tomcat_home/bin/shutdown.sh
STARTUP=$tomcat_home/bin/startup.sh
app_home=$tomcat_home/$app_name
app_tar=${app_name}.tar.gz
app_bak=$app_home/bak
install_dir=/home/tomcat
time_stamp=`date +%Y%m%d-%H%M%S`
#回滚方法
function rollback(){
    echo ">>> Install rollback."
    bak_file=`ls ${app_bak} -t | head -1 |grep -v 'total'`
    bak_path=$app_bak/$bak_file
    echo ">>> Rollback file : $bak_path."
    for file in $app_home/*
        do
            if [ "$file" != "$app_bak" -a "$file" != "$app_home" ]
            then
                rm -rf $file
            fi
        done
    mv $bak_path/* $app_home
    echo ">>> Remove rollback directory."
    rm -rf $bak_path
    echo "<<< Rollback complete."
}
#创建app_home目录
function app_homedir(){
    if [ ! -e $app_home ]
    then
        echo ">>> Make directory $app_home"
        mkdir $app_home
            if [ $? != 0 ]
    then 
        echo ">>> Install fail,please check the log"
        exit 1
    fi
    fi
}
#备份app路径下除bak外的所有文件
function bak_app(){
    echo ">>> Backup app"
    if [ ! -e $app_bak ]
    then
        echo ">>> Make directory $app_bak"
        mkdir $app_bak
            if [ $? != 0 ]
    then 
        echo ">>> Install fail,please check the log"
        exit 1
    fi
    fi
    bak_name=`find $app_home -maxdepth 1 -name "ver.*" -printf "%f\n"`
    if [ 0 == ${bak_name:-0} ]
    then    
        path_bak=$app_bak/first_bak
    else
        array=( `echo $bak_name | tr '.' ' '` )
        path_bak=$app_bak/${array[1]}
    fi
    if [ ! -e $path_bak ]
    then
        echo ">>> Make directory $path_bak"
        mkdir $path_bak
            if [ $? != 0 ]
    then 
        echo ">>> Install fail,please check the log"
        exit 1
    fi
    fi
    for file in $app_home/*
        do
            if [ "$file" != "$app_bak" -a "$file" != "$app_home" ]
            then
                mv $file $path_bak
            fi
        done
    echo "<<< Backup app complete"
}
#解压到app路劲下
function install_app(){
    echo ">>> Install $app_name start"
    #部署日期标识
    touch $app_home/ver.${time_stamp}
    if [ $? != 0 ]
    then 
        echo ">>> install fail,install will rollback"
        return 1
    fi
    if [ ! -e $install_dir/$app_tar ]
    then
        rollback
        echo "<<< App install file is not exist,please check the app install file"
        exit 1
    fi
    tar -zxf $install_dir/$app_tar -C $app_home
    if [ $? != 0 ]
    then 
        echo ">>> install fail,install will rollback"
        return 1
    fi
    rm -f $install_dir/$app_tar
    if [ $? != 0 ]
    then 
        echo ">>> install fail,install will rollback"
        return 1
    fi
    echo "<<< Install $app_name complete"
}
#安装tomcat文件
function install_webapps(){
    echo ">>> install webapps start"
    if [ ! -e $tomcat_webapp ]
    then
        echo ">>> Make directory $tomcat_webapp"
        mkdir $tomcat_webapp
            if [ $? != 0 ]
    then 
        echo ">>> Install fail,please check the log"
        exit 1
    fi
    else 
        echo ">>> Remove $tomcat_webapp"
        rm -rf $tomcat_webapp/*
            if [ $? != 0 ]
    then 
        echo ">>> Install fail,please check the log"
        exit 1
    fi
    fi
    if [ ! -e $install_dir/tomcat-app.tar.gz ]
    then
        echo "<<< App install file is not exist,please check the webapps install file"
            if [ $? != 0 ]
    then 
        echo ">>> Install fail,please check the log"
        exit 1
    fi
    fi
    tar -zxf $install_dir/tomcat-app.tar.gz -C $tomcat_webapp
    rm -f $install_dir/tomcat-app.tar.gz
    echo "<<< install webapps complete"
}
#修改tomcat中web.xml配置文件,指定目录为app_home目录
function update_config(){
    echo ">>> Update $tomcat_webapp/WEB-INF/web.xml"
    sed -i "s!\${sysprop_home}!${app_home}\/config\/system.properties!g" $tomcat_webapp/WEB-INF/web.xml
    echo "<<< Update the vframe-home to $app_home/config/system.properties"
}
#删除多余备份,只保留3个文件
function bak_remove(){
    if [ -e $app_bak ]
    then
        bak_num=`ls ${app_bak} -l | awk '/^d/' | wc -l`
        if [ $bak_num -gt 3 ]
            then
                del_num=`expr ${bak_num} - 3`
                for bak_file in `ls ${app_bak} -tr | head -${del_num} |grep -v 'total'`
                do
                    echo ">>> Delete bak $app_bak/$bak_file"
                    rm -rf  $app_bak/$bak_file
                done
        fi
    fi
}
#关闭tomcat
function kill_tomcat(){
    echo ">>> Tomcat shutdown."
    sh $SHUTDOWN
    sleep 2
    pidList=$(ps aux | grep $tomcat_home | grep -v grep | awk '{print $2}')
    for pid in $pidList
    do
        kill -9 $pid
            if [ $? != 0 ]
    then 
        echo ">>> Install fail,please check the log"
        exit 1
    fi
        echo ">>> Kill the process [$pid] successfully."
    done
    echo "<<< Tomcat shutdown complete"
}
#启动tomcat
function start_tomcat(){
    echo ">>> Tomcat startup"
    sh $STARTUP
    if [ $? != 0 ]
    then
        echo "<<< Fail : Tomcat restart failure."
        exit 1
    fi
    echo "<<< Tomcat startup complete."
}
echo "=================$app_name install start================="
echo ">>> Timestamp:$time_stamp"
kill_tomcat
app_homedir
bak_app
install_app
if [ $? != 0 ]
then 
    rollback
    echo "Install fail,please check the log"
    exit 1
fi
install_webapps
update_config
bak_remove
start_tomcat
echo "=================$app_name install done================="

 

posted on 2019-03-13 15:00  ScoutDu  阅读(178)  评论(0编辑  收藏  举报