1. 程式人生 > >自動部署jar應用的shell指令碼

自動部署jar應用的shell指令碼

寫的一個自動部署jar應用的shell指令碼,如果有哪些地方不合適的還請各位大佬指正

#!/bin/sh
#use deployjar.sh [-f ${jar file name}] [-d ${jar belong directory} [-s -m -l -xl -xxl]]

JARFILENAME=
DIRECTORY=
mx=512m
mn=128m
page=64m
index=0
parameters=("[email protected]")
for i in $*
do
    case $i in
        -f)
         JARFILENAME=${parameters[index+1]}
         echo "jar file name is $JARFILENAME"
         ;;
        -d)
         DIRECTORY=${parameters[index+1]}
         echo "jar belong directory is $DIRECTORY"
         ;;
        -s)
         mx=384m
         mn=96m
         page=32m
         ;;
        -m)
         mx=512m
         mn=128m
         page=64m
         ;;
        -l)
         mx=768m
         mn=192m
         page=64m
         ;;
        -xl)
         mx=1g
         mn=256m
         page=128m
         ;;
        -xxl)
         mx=2g
         mn=512m
         page=256m
         ;;
        *)
        echo "option ${parameters[index-1]} : ${parameters[index]}"
    esac
    let index+=1
done

echo "filename : $JARFILENAME directory : $DIRECTORY"

if [ -z "$JARFILENAME" ]
then
        echo "jar file name is null"
        exit 0
fi
if [ -z "$DIRECTORY" ]
then
        echo "jar belong directory is null"
        exit 0
fi

pid=`ps aux | grep java |grep -w "$JARFILENAME" | grep -v grep | awk '{print $2}'`

echo "'$JARFILENAME'.jar pid is '$pid'"
if [ "$pid" != "" ]; then
    kill $pid
fi
flag=0
count=0
while  [ $flag -lt 1 ]; do
    pid=`ps aux | grep java |grep -w "$JARFILENAME" | grep -v grep | awk '{print $2}'`
    if [ "$pid" != "" ]; then
       if [ $count -lt 10 ]; then
           sleep 1s
           echo "jar is stoping, waiting $((count++)) seconds..."
       else
           kill -9 $pid
       fi 
    else  
        flag=1
        echo "jar is stopped"
    fi
done

echo "execute jar file path $DIRECTORY/$JARFILENAME.jar"

#g1gc with log
#nohup java -server -Xms1g -Xmx1g -XX:MaxMetaspaceSize=256m -XX:MetaspaceSize=256m -Xss256k  -XX:+UseFastAccessorMethods \
#  -XX:+DisableExplicitGC -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+ExplicitGCInvokesConcurrent \
#  -Djava.awt.headless=true -Xloggc:$DIRECTORY/$JARFILENAME-gc.log -verbose:gc -XX:+PrintGCDetails -XX:LargePageSizeInBytes=64m \
#  -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=1 -XX:GCLogFileSize=100M \
#  -XX:+UseBiasedLocking -jar $DIRECTORY/$JARFILENAME.jar >$DIRECTORY/$JARFILENAME.log 2>&1 &

#g1gc without log
#nohup java -server -Xms1g -Xmx1g -XX:MaxMetaspaceSize=256m -XX:MetaspaceSize=256m -Xss256k  -XX:+UseFastAccessorMethods \
#  -XX:+DisableExplicitGC -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+ExplicitGCInvokesConcurrent \
#  -XX:LargePageSizeInBytes=64m -XX:+UseBiasedLocking -jar $DIRECTORY/$JARFILENAME.jar >$DIRECTORY/$JARFILENAME.log 2>&1 &
   
nohup java -server -Xms$mx -Xmx$mx -Xmn$mn -XX:MaxMetaspaceSize=$mn -XX:MetaspaceSize=$mn -Xss256k -XX:TargetSurvivorRatio=90 \
   -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:LargePageSizeInBytes=$page -XX:+UseFastAccessorMethods \
   -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:-OmitStackTraceInFastThrow -XX:+UseBiasedLocking \
   -XX:ParallelGCThreads=20 -XX:+UseParNewGC -XX:SurvivorRatio=8 -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=15 \
   -jar $DIRECTORY/$JARFILENAME.jar >$DIRECTORY/$JARFILENAME.log 2>&1 &