标签:
运维经验分享作为一个专题,目前共7篇文章
====================================分割线======================================
刚接到开发人员要求,今晚上线新的ChatterServer版本,新版本目录存在一些变化,要求我出一个新的服务控制脚本,因此在前两版的基础上有了第三版,前两版地址《运维经验分享(一)-- Linux Shell之ChatterServer服务控制脚本》和《运维经验分享(二)-- Linux Shell之ChatterServer服务控制脚本二次优化》
增加softlink invoke和今天改良的shell脚本中的一些好的方法和技巧,今天改良的shell脚本请参考文章《运维经验分享(五)-- 改进的java进程管理的服务控制脚本》。
有几个已知的问题:
BASENAME因为时间原因使用了绝对路径,后期将更新新版
log日志的某些处理上有些多余,将于后期更新新版
其他问题欢迎各位网友、博友批评指正和留言!
现将脚本内容粘贴如下:
#!/bin/bash #chkconfig: 345 86 14 #description: Startup and shutdown script for ChatterServer(Port:$SERVICEPORT) # Public header # resolve links - $0 may be a softlink PRG="$0" while [ -h "$PRG" ]; do ls=`ls -ld "$PRG"` link=`expr "$ls" : ‘.*-> \(.*\)$‘` if expr "$link" : ‘/.*‘ > /dev/null; then PRG="$link" else PRG=`dirname "$PRG"`/"$link" fi done # Get standard environment variables PRGDIR=`dirname "$PRG"` # Public header WORKDIR=$PRGDIR # end public header # -Public header #BASEDIR=.. BASEDIR=/data/chatter VERSION=1.0.0-snapshot SERVER=$BASEDIR/chatter-$VERSION\.jar LOGDIR=$BASEDIR/logs if [[ ! -d $LOGDIR ]]; then mkdir $LOGDIR fi PORTFILE=$BASEDIR/conf/constant.properties # TODO # reslove dos2unix packages SERVICEPORT=$(cat $PORTFILE | dos2unix | grep ^port | awk -F ‘=‘ ‘{print $2}‘) PIDFILE=$LOGDIR/chatter.pid BASENAME=chatter ARGS="-Xms4g -Xmx8g -Xmn8g -Xss256k -XX:MaxPermSize=64m -XX:-UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=30 -XX:SurvivorRatio=6" # -Xms2g -Xmx2g -Xmn2g -Xss128k -XX:MaxPermSize=64m -XX:-UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=30 -XX:SurvivorRatio=6 status() { if [[ ! -f $PIDFILE ]]; then echo "ERROR: ChatterServer(Port:$SERVICEPORT) pid file is NOT exist" exit 1 fi if [[ ! -d /proc/$(cat $PIDFILE) ]]; then echo "ERROR: ChatterServer(Port:$SERVICEPORT) is NOT running" exit 1 else echo "SUCCESS: ChatterServer(Port:$SERVICEPORT) pid($(cat $PIDFILE)) is OK" fi # The judgment priority: pid > port > piffile # netstat run by common user will get some error output, so we put those error outout to /dev/null if [[ $(netstat -anop 2>/dev/null | grep $SERVICEPORT | grep LISTEN) ]];then echo "SUCCESS: ChatterServer(Port:$SERVICEPORT) is OK" else echo "ERROR: ChatterServer(Port:$SERVICEPORT) port is NOT listen" exit 1 fi } start() { if [[ -e $PIDFILE ]]; then if [[ -d /proc/$(cat $PIDFILE) ]]; then echo "ERROR: pidfile $PIDFILE exist, ChatterServer(Port:$SERVICEPORT) has started with pid $(cat $PIDFILE)" ## pid file can be deleted #/bin/rm -f $PIDFILE exit 1 fi fi if [[ -e $SERVER ]]; then echo "INFO: Starting ChatterServer(Port:$SERVICEPORT)" # Start ChatterServer core daemon # Why using "date +"%Y%m%d""? Because we just need restart this once per day # For ChatterServer wiil find some file in $BASEDIR #cd $LOGDIR/../ cd $BASEDIR ## TODO ## Because current directory has changed #SERVER=./chatter-$VERSION\.jar #LOGDIR=./logs #nohup java -jar $SERVER $ARGS >>$LOGDIR/console-$(date +"%Y%m%d").out 2>&1 & if java -jar $SERVER $ARGS >>$LOGDIR/console.out 2>&1 & then sleep 2 if [[ -d /proc/$! ]]; then echo $! > $PIDFILE echo "SUCCESS: ChatterServer(Port:$SERVICEPORT) start OK" echo "[ $(date +"%D %T") ] SUCCESS: ChatterServer(Port:$SERVICEPORT) started with pid $(cat $PIDFILE) " >>$LOGDIR/service.log fi elif java -jar $SERVER $ARGS >>$LOGDIR/console.out 2>&1 & then sleep 2 if [[ -d /proc/$! ]]; then echo $! > $PIDFILE echo "SUCCESS: ChatterServer(Port:$SERVICEPORT) start OK" echo "[ $(date +"%D %T") ] SUCCESS: ChatterServer(Port:$SERVICEPORT) started with pid $(cat $PIDFILE) " >>$LOGDIR/service.log fi else echo "ERROR: ChatterServer(Port:$SERVICEPORT) start failed" # Setting up start log echo "[ $(date +"%D %T") ] ERROR: ChatterServer(Port:$SERVICEPORT) start failed " >>$LOGDIR/service.log exit $RETVAL fi else echo "ERROR: Couldn‘t find $SERVER" # TODO We just think this is not essential # Do NOT setting up log here exit 1 fi } stop() { if [[ -e $PIDFILE ]]; then pid=$(cat $PIDFILE) #if kill -TERM $PIDFILE >/dev/null 2>&1 # TODO remove debug info #echo "DEBUG: $LOGDIR/console-$(date +"%Y%m%d").out" # Ubuntu can NOT use "usleep", so use "sleep" instead # usleep 100000 if kill -TERM $pid >>$LOGDIR/console-$(date +"%Y%m%d").out && sleep 1 then if test -d /proc/$(cat $PIDFILE) then sleep 60 else echo "SUCCESS: ChatterServer(Port:$SERVICEPORT) stop OK with TERM" # Setting up stop log echo "[ $(date +"%D %T") ] SUCCESS: ChatterServer(Port:$SERVICEPORT) stop OK with TERM " >>$LOGDIR/service.log # Because we can NOT use usleep , so we must comment out sleep 1 next #sleep 1 # Ubuntu can NOT use "usleep", so use "sleep" instead # usleep 100000 # Remove pid file /bin/rm $PIDFILE fi elif kill -KILL $pid >/dev/null 2>&1 && sleep 1 then if test -d /proc/$(cat $PIDFILE) then sleep 60 else echo "SUCCESS: ChatterServer(Port:$SERVICEPORT) stop OK with KILL" # Setting up stop log echo "[ $(date +"%D %T") ] SUCCESS: ChatterServer(Port:$SERVICEPORT) stop OK with KILL " >>$LOGDIR/service.log # Because we can NOT use usleep , so we must comment out sleep 1 next #sleep 1 # Remove pid file /bin/rm $PIDFILE fi elif kill -9 `cat $PIDFILE` >>$LOGDIR/console.out 2>&1 then while [[ -d /proc/$(cat $PIDFILE) ]]; do leep 1 done if test -d /proc/$(cat $PIDFILE) then echo "server stop failed" exit 1 else echo "SUCCESS: ChatterServer(Port:$SERVICEPORT) stop OK with KILL" # Setting up stop log echo "[ $(date +"%D %T") ] SUCCESS: ChatterServer(Port:$SERVICEPORT) stop OK with KILL " >>$LOGDIR/service.log # Because we can NOT use usleep , so we must comment out sleep 1 next #sleep 1 # Remove pid file /bin/rm $PIDFILE fi else echo "ERROR: ChatterServer(Port:$SERVICEPORT) stop faild" # Setting up stop log echo "[ $(date +"%D %T") ] ERROR: ChatterServer(Port:$SERVICEPORT) stop failed " >>$LOGDIR/service.log exit 1 fi else echo "ERROR: No ChatterServer(Port:$SERVICEPORT) running" # TODO We just think this is not essential # Do NOT setting up log here exit 1 fi } restart() { echo "INFO: Restarting ChatterServer(Port:$SERVICEPORT)" stop # # Those lines will remove in next release # if [[ $(netstat -anop 2>/dev/null | grep $SERVICEPORT | grep LISTEN) ]]; then # echo "WARNNING: port $SERVICEPORT is in using, must waiting" # sleep 5 # if [[ $(netstat -anop 2>/dev/null | grep $SERVICEPORT | grep LISTEN) ]]; then # echo "WARNNING : port $SERVICEPORT is still in using, must waiting" # sleep 2 # fi # fi # -Those lines will remove in next release # Do NOT using sleep any seconds here with stop() function used start } case $1 in status) status ;; start) start ;; stop) stop ;; restart) restart ;; help|*) echo "Usage: $0 {status|start|stop|restart|help} with $0 itself" exit 1 ;; esac # replace "exit 0" with ":" #exit 0 :
====================================分割线======================================
运维经验分享作为一个专题,目前共7篇文章
本文出自 “通信,我的最爱” 博客,请务必保留此出处http://dgd2010.blog.51cto.com/1539422/1677214
运维经验分享(七)-- Linux Shell之ChatterServer服务控制脚本第三次优化
标签:
原文地址:http://dgd2010.blog.51cto.com/1539422/1677214