Tomcat调优及启动脚本

文章
林里克斯

Linux下对Tomcat进行调优及备份升级部署脚本编写

实验平台:CentOS 6.8
实验IP:10.113.128.1
Tomcat版本:

Tomcat调优及启动脚本



一、JAVA_OPTS参数


JAVA_OPTS="
-server             #一定要作为第一个参数,在多个CPU时性能佳
-Xms1024m           #初始Heap堆大小,使用的最小内存,cpu性能高时此值应设的大一些
-Xmx1024m           #java heap最大值,使用的最大内存
#上面两个值是分配JVM的最小和最大内存,取决于硬件物理内存的大小,建议均设为物理内存的一半。
-XX:PermSize=169m   #设定内存的永久保存区域
-XX:MaxPermSize=169m    #设定最大内存的永久保存区域
-Xmn678m        #young generation的heap大小,一般设置为Xmx的3、4分之一
-Xss256K        #每个线程的Stack大小
-XX:+CMSClassUnloadingEnabled
-XX:+UseParNewGC        #缩短minor收集的时间
-XX:+CMSParallelRemarkEnabled
-XX:SoftRefLRUPolicyMSPerMB=0
-XX:+UseFastAccessorMethods
-XX:+UseConcMarkSweepGC         #缩短major收集的时间
-XX:+DisableExplicitGC
-XX:ParallelGCThreads=10
-XX:CMSFullGCsBeforeCompaction=0
-XX:+UseCMSCompactAtFullCollection
-XX:SurvivorRatio=4
-XX:LargePageSizeInBytes=128m
-XX:+UseCMSInitiatingOccupancyOnly
-XX:CMSInitiatingOccupancyFraction=70
-XX:+UseCompressedOops
-XX:+CMSScavengeBeforeRemark
-XX:MaxTenuringThreshold=15
-XX:TargetSurvivorRatio=90
-XX:+HeapDumpOnOutOfMemoryError
-XX:+ShowMessageBoxOnError
-Djava.rmi.server.hostname=10.113.128.1     #使用JAVA自带工具查看jvm使用情况
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=10001
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-XX:+UnlockCommercialFeatures
-XX:+FlightRecorder
-Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000
-Dsun.lang.ClassLoader.allowArraySyntax=true
-Djava.awt.headless=true"
#待续。。。

二、Tomcat几个通用启动脚本


1.tomcat备份更新脚本

#!/bin/bash
#Version: 1.0.1
#Editor:JarboU
#Date:2016.12.15

DATE=$(date +%Y%m%d%H%M)
pkill -9 -f tomcat
sleep 5
echo "tomcat关闭"
#中间件位置
SoftPath=/www/tomcat
#备份目录
BakPath=/www/bak
#更新目录
UpdatePath=/www/update
#模块应用名字
AppName=BS
#jdk目录
JdkPath=/usr/java/jdk1.7.0_67

export JAVA_HOME="$JdkPath"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "Tomcat与JDK版本:"
cd $SoftPath/bin
./version.sh
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++"
export JAVA_OPTS="-server -Xms2048m -Xmx2048m -XX:PermSize=512m -XX:MaxPermSize=512m -Xmn512m -Xss256K   -XX:+CMSClassUnloadingEnabled  -XX:+UseParNewGC  -XX:+CMSParallelRemarkEnabled -XX:SoftRefLRUPolicyMSPerMB=0  -XX:+UseFastAccessorMethods -XX:+UseConcMarkSweepGC -XX:+DisableExplicitGC -XX:ParallelGCThreads=10  -XX:CMSFullGCsBeforeCompaction=0 -XX:+UseCMSCompactAtFullCollection -XX:SurvivorRatio=1  -XX:LargePageSizeInBytes=128m -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCompressedOops -XX:+CMSScavengeBeforeRemark  -XX:MaxTenuringThreshold=15 -XX:TargetSurvivorRatio=90 -XX:+HeapDumpOnOutOfMemoryError -XX:+ShowMessageBoxOnError
-Djava.rmi.server.hostname=10.113.128.1
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=10001
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Dsun.rmi.dgc.client.gcInterval=3600000
-Dsun.rmi.dgc.server.gcInterval=3600000
-Dsun.lang.ClassLoader.allowArraySyntax=true
-Djava.awt.headless=true
"
echo "执行备份"
mkdir $BakPath/$AppName$DATE -p
cp -r $SoftPath/webapps/ROOT.war $BakPath/$AppName$DATE -p
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "代码文件对比:"
find $SoftPath/webapps/ROOT -type f | xargs md5sum   > $BakPath/$DATE/tomcat.diff 2> /dev/null
find $UpdatePath/ROOT -type f | xargs md5sum  > $BakPath/$DATE/update.diff 2> /dev/null
sed -i 's/\/ane\/update\//\/ane\/tomcat\/webapps\//g' $BakPath/$DATE/update.diff
diff $BakPath/$DATE/tomcat.diff $BakPath/$DATE/update.diff > $BakPath/$DATE/diff.txt
Diff=`cat $BakPath/$DATE/diff.txt | wc -l`
echo $Diff
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "执行备份成功,开始清理缓存"
rm -rf $SoftPath/temp/*
rm -rf $SoftPath/work/*
rm -rf $SoftPath/webapps/*
echo "缓存清除完成,项目更新开始"
cp -r $UpdatePath/ROOT.war $SoftPath/webapps/
echo "项目启动中,请稍后....."
cd $SoftPath/bin/
nohup ./startup.sh -Dfile.encoding=utf-8 > /dev/null 2>&1 &
echo "项目启动完成!"
tail -500f $SoftPath/logs/catalina.out
#集成备份,更新功能

2.tomcat备份更新脚本

#!/bin/bash
#Version: 2.0.0
#Editor:JarboU
#Date:2017.02.09

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
DATE=$(date +%Y%m%d%H%M)
PIDFILE=/tmp/tomcat.pid
#################配置参数###########################
#中间件位置
SoftPath=/www/tomcat
#备份目录
BakPath=/www/bak
#更新目录
UpdatePath=/www/update
#模块编码
AppName=blog
#JDK目录
JdkPath=/usr/java/jdk1.7.0_67
#进程名称
JDK_NAME=java
#应用包名
WarName=ROOT.war
#jvm监听端口
JxmPort=10001

#################应用参数###########################
HostIP=`ip addr | grep inet.*/24 | awk -F " " '{print $2 }' | awk -F "/" '{print $1}'`
HostMenm=`free -m | grep ^Mem\: |awk -F " " '{print $4}'`
JMenm="`expr $HostMenm \* 75 / 100`m"
JGCMenm="`expr $HostMenm \* 75 / 100 / 8 \* 3`m"
JPermSize="`expr $HostMenm \* 75 / 100 / 8 \* 3 / 4`m"
JAVAOPTS=" -server -Xms$JMenm -Xmx$JMenm -XX:PermSize=$JPermSize -XX:MaxPermSize=$JPermSize -Xmn$JGCMenm -Xss256K   -XX:+CMSClassUnloadingEnabled  -XX:+UseParNewGC  -XX:+CMSParallelRemarkEnabled -XX:SoftRefLRUPolicyMSPerMB=0  -XX:+UseFastAccessorMethods -XX:+UseConcMarkSweepGC -XX:+DisableExplicitGC -XX:ParallelGCThreads=10  -XX:CMSFullGCsBeforeCompaction=0 -XX:+UseCMSCompactAtFullCollection -XX:SurvivorRatio=4  -XX:LargePageSizeInBytes=128m -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCompressedOops -XX:+CMSScavengeBeforeRemark  -XX:MaxTenuringThreshold=15 -XX:TargetSurvivorRatio=90 -XX:+HeapDumpOnOutOfMemoryError -XX:+ShowMessageBoxOnError -Djava.rmi.server.hostname=$HostIP -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JxmPort -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Dsun.lang.ClassLoader.allowArraySyntax=true -Djava.awt.headless=true"
#################代码区域###########################
if [ -f $PIDFILE  ] && [ -s $PIDFILE ]
        then
        PID=`cat $PIDFILE`

        if [ "x$PID" != "x" ] && kill -0 $PID 2>/dev/null && [ $JDK_NAME == `ps -e | grep $PID | awk '{print $4}'` ]
        then
                STATUS="tomcat (pid `pidof tomcat`) running.."
                RUNNING=1
        else
                rm -f $PIDFILE
                STATUS="tomcat (pid file existed ($PID) and now removed) not running.."
                RUNNING=0
        fi
else
        if [ `ps -e | grep $JDK_NAME | head -1 | awk '{ print $1 }'` ]
                then
                STATUS="tomcat (pid `pidof $APP`, but no pid file) running.."
        else
                STATUS="tomcat (no pid file) not running"
        fi
        RUNNING=1
fi

start() {
        if [ $RUNNING -eq 1 ]
                then
        export JAVA_HOME="$JdkPath"
        echo "JDK版本"
        $JAVA_HOME/bin/java -version
        pid=`ps -ef |grep $SoftPath | grep -v grep | awk '{print $2}'`
        kill -9 $pid
        export JAVA_OPTS="$JAVAOPTS"
        rm -rf $SoftPath/temp/*
        rm -rf $SoftPath/work/*
        rm -rf $SoftPath/webapps/*
        echo "缓存清除完成,项目更新开始"
        cp -r $UpdatePath/$WarName $SoftPath/webapps/$WarName
        echo "项目启动中,请稍后....."
        cd $SoftPath/bin/
        nohup ./startup.sh -Dfile.encoding=utf-8 > /dev/null 2>&1 &
        echo "项目启动完成!"
        tail -500f $SoftPath/logs/catalina.out
        else
                echo "启动失败请检查配置文件!"
                rm -rf /tmp/tomcat.pid
        fi
}

stop() {
    export JAVA_HOME="$JdkPath"
        #export JAVA_OPTS="$JAVAOPTS"
        $SoftPath/bin/catalina.sh stop
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ]
        RUNNING=1
        echo "tomcat关闭"

}
backup() {
    echo "执行备份"
    mkdir $BakPath/$AppName$DATE -p
    chown -R ane.ane $BakPath
    cp -r $SoftPath/webapps/$WarName $BakPath/$AppName$DATE/
    echo "执行备份成功!"
}

case "$1" in
        start)
        backup
                start
                ;;
        stop)
                stop
                ;;
        backup)
                backup
                ;;
        restart)
                stop
        sleep 5
        backup
                start
                ;;
esac

exit 0

Over~

版权协议须知!

本篇文章来源于 Uambiguous ,如本文章侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意

1182 0 2017-02-10


分享:
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
博主卡片
林里克斯 博主大人
一个致力于Linux的运维平台
运维时间
搭建这个平台,只为分享及记载自己所遇之事和难题。

现在时间 2024-12-28

今日天气
站点统计
  • 文章总数:241篇
  • 分类总数:29个
  • 评论总数:12条
  • 本站总访问量 353465 次

@奥奥

@Wong arrhenius 牛比

@MakerFace 厉害了!