最近开发人员提出新的需求,想将ChatterServer服务控制脚本放到ChatterServer根目录下的bin目录下,并向一次性可以部署多个实例,因此这一个需求引发了多个改变:
1.不再使用service进行控制,仍然保留status、start、stop和restart功能
2.BASEDIR不再使用绝对路径而要使用相对路径,例如BASEDIR=..
3.SERVICEPORT要从ChatterServer根目录下的conf目录中的文件中读取
4.日志目录logs将不再手动创建,控制脚本需要自己创建logs目录
针对以上4点变更,改进如下
1.去掉service控制,只需要将此脚本放到ChatterServer根目录下的bin目录下执行(实际上更简单了)
2.既然要使用相对路径而不使用绝对路径就要正确设置Shell脚本的当前工作目录,否则非但脚本不好用(log和pid都无法正常生成),还会导致ChatterServer的核心命令行无法使用(无法读取到相对路径下的配置文件)
3.要想SERVICEPORT从ChatterServer根目录下的conf目录中的文件中读取,非常简单,但疑难问题在于排查上,例如grep对dos文件格式(file format)的文件非常不友好,会对脚本中后面的命令产生巨大影响(特别是echo以及变量(以$开头的字符串)几乎会乱掉),因此在使用grep前,必须将此文件的内容通过dos2unix命令来转化,除非开发人员乐意将文件格式从dos手动改为unix(可以参考《UNIX/Linux环境编程必须需要注意的问题总结》)
4.相当于多了一层异常检测而已,比较简单,看新的ChatterServer服务控制脚本就会明白。
经过二次“优化”改造的脚本如下:
#!/bin/bash
#chkconfig: 345 86 14
#description: Startup and shutdown script for ChatterServer(Port:$SERVICEPORT))
VERSION=1.0.0-snapshot
BASEDIR=..
# choose LOGDIR as standard of work directory
LOGDIR=$(pwd)/$BASEDIR/logs
if [[ ! -d $LOGDIR ]]; then
mkdir $LOGDIR
fi
#SERVICEPORT=29093
#SERVICEPORT=`grep ^port $(pwd)/../conf/constant.properties | awk -F ‘=‘ ‘{print $2}‘`
# NOTE: $(pwd)/../conf/constant.properties file fileformat must be unix NOT dos in shell scripts, or will cause some unknown error
# NOTE: grep is vrey sensitive to dos fileformat or unix fileformat
# apt-get install dos2unix
PORTFILE=$(pwd)/../conf/constant.properties
SERVICEPORT=$(cat $PORTFILE | dos2unix | grep ^port | awk -F ‘=‘ ‘{print $2}‘)
PIDFILE=$LOGDIR/chatter.pid
SERVER=$(pwd)/$BASEDIR/chatter-$VERSION\.jar
BASENAME=chatter
ARGS="-Xms2g -Xmx4g -Xmn4g -Xss128k -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() {
# 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) || -f $PIDFILE ]];then
#pid=$(cat $PIDFILE)
pid=$(ps -ef | grep java | grep $BASENAME | grep -v grep | awk ‘{print $2}‘)
if [[ $pid != "" && $(ps -ef | grep $pid | grep -v grep) ]]; then
echo "SUCCESS: ChatterServer(Port:$SERVICEPORT) is OK"
exit 0
else
echo "ERROR: ChatterServer(Port:$SERVICEPORT) pid is NOT exist"
exit 1
fi
elif [[ ! $(netstat -anop 2>/dev/null | grep $SERVICEPORT | grep LISTEN) ]]; then
echo "ERROR: ChatterServer(Port:$SERVICEPORT) port is NOT listen"
exit 1
elif [[ ! -f $PIDFILE ]]; then
echo "ERROR: ChatterServer(Port:$SERVICEPORT) pid file is NOT exist"
exit 1
else
echo "ERROR: ChatterServer(Port:$SERVICEPORT) is NOT running"
exit 1
fi
}
start() {
if [[ -e $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
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/../
#nohup java -jar $SERVER $ARGS >>$LOGDIR/console-$(date +"%Y%m%d").out 2>&1 &
java -jar $SERVER $ARGS >>$LOGDIR/console-$(date +"%Y%m%d").out 2>&1 &
#java -jar $SERVER $ARGS >$LOGDIR/console.out 2>&1 &
RETVAL=$?
# shell do NOT need home directory
## For ChatterServer wiil find some file in $BASEDIR
cd $LOGDIR/../bin
if [[ $RETVAL -eq 0 ]]; then
## $! --> Expands to the process ID of the most recently executed background (asynchronous) command.
#echo $! > $PIDFILE
# For java performance issue, port 29092 will listen latter, we will waiting for 2 second
sleep 2
# get pid var
# TODO remove debug info
#echo "DEBUG: "
#ps -ef | grep $BASENAME | grep -v grep | awk ‘{print $2}‘
# end debug
#pid=$(ps -ef | grep java | grep $BASENAME | grep -v grep | awk ‘{print $2}‘)
pid=$(netstat -anop 2>/dev/null | grep $SERVICEPORT | grep LISTEN | awk ‘{print $7}‘ | awk -F ‘/‘ ‘{print $1}‘)
# send pid number to pid file
echo $pid > $PIDFILE
# Those lines will remove in next release
# TODO remove debug info
#echo "DEBUG: live 1"
# For java performance issue, port 29092 will listen latter, so we change judgment conditions
if [[ $(netstat -anop 2>/dev/null | grep $SERVICEPORT | grep LISTEN) || -f $PIDFILE ]]; then
echo "SUCCESS: ChatterServer(Port:$SERVICEPORT) start OK"
# Setting up start log
echo "[ $(date +"%D %T") ] SUCCESS: ChatterServer(Port:$SERVICEPORT) started with pid $(cat $PIDFILE) " >>$LOGDIR/service.log
fi
# TODO remove debug info
#echo "DEBUG: live 2"
# -Those lines will remove in next release
#echo "SUCCESS: ChatterServer start OK"
## Setting up start log
#echo "[ $(date +"%D %T") ] SUCCESS: ChatterServer started with pid $(cat $PIDFILE) " >>$LOGDIR/service.log
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
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
elif kill -KILL $pid >/dev/null 2>&1 && sleep 1
then
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
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
# Remove pid file
if [[ -f $PIDFILE ]]; then
/bin/rm -f $PIDFILE
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
:--end--
本文出自 “通信,我的最爱” 博客,请务必保留此出处http://dgd2010.blog.51cto.com/1539422/1674188
Linux Shell之ChatterServer服务控制脚本二次优化
原文:http://dgd2010.blog.51cto.com/1539422/1674188