运维经验分享(一)– Linux Shell之ChatterServer服务控制脚本

运维经验分享作为一个专题,目前共7篇文章

《运维经验分享(一)– Linux Shell之ChatterServer服务控制脚本》

《运维经验分享(二)– Linux Shell之ChatterServer服务控制脚本二次优化》

《运维经验分享(三)– 解决Ubuntu下crontab不能正确执行Shell脚本的问题(一)》

《运维经验分享(四)–关于 java进程管理的服务控制脚本编程思路分析》

《运维经验分享(五)– 改进的java进程管理的服务控制脚本》

《运维经验分享(六)– 深究crontab不能正确执行Shell脚本的问题(二)》

《运维经验分享(七)– Linux Shell之ChatterServer服务控制脚本第三次优化》

====================================分割线======================================

本脚本是ChatterServer的服务控制脚本,即能通过service命令来控制ChatterServer的启动、停止、重新启动以及状态查看,就像mysql有/etc/init.d/mysql或/etc/init.d/mysqld一样,只是这个ChatterServer的服务控制脚本写起来更加困难,有些信息的捕获和判断更加复杂,原因还是主要与ChatterServer的运行方式和启动过程中发生的一些事情有关,这个在脚本的注释中已经充分的标注清楚了。

由于ChatterServer运行在Ubuntu上,因此跟CentOS还是有很大的差异,例如sleep 1和usleep 100000上,CentOS能控制更精确的延时,而Ubuntu不能支持usleep因此不能控制更精确的延时,导致潜在的性能问题。

为方便阅读,在此简单举几个问题例子:

1.ChatterServer的java核心命令行执行后,可能是由于程序或者性能原因,会延迟建立端口连接;

2.ChatterServer的jar包里面用到了一些配置文件,在特定的目录下,必须先进入这些目录才能执行java核心命令行;

问题解决办法:

针对第一个问题,采用多重判断和延时执行的方法;

针对第二个问题,调试时带来问题较多,后来才与开发沟通,弄明白其原因;

此脚本还根据开发的需求,将ChatterServer日志按照日期保存以便于在服务状态发生改变时不会新的日志覆盖,将脚本运行日志保存到新的日志,共管理查看。

以下是脚本第四版内容:

#!/bin/bash#chkconfig:3458614#description:StartupandshutdownscriptforChatterServerVERSION=1.0.0-snapshotBASEDIR=/data/chatterserverLOGDIR=$BASEDIR/logsSERVICEPORT=29092PIDFILE=$BASEDIR/pid/chatter.pidSERVER=$BASEDIR/chatter-$VERSION\.jarBASENAME=chatter#-Xms2g-Xmx2g-Xmn2g-Xss128k-XX:MaxPermSize=64m-XX:-UseParallelGC-XX:+UseParallelOldGC-XX:ParallelGCThreads=4-XX:+UseConcMarkSweepGC-XX:MaxTenuringThreshold=30-XX:SurvivorRatio=6ARGS=””status(){#Thejudgmentpriority:pid>port>piffile#netstatrunbycommonuserwillgetsomeerroroutput,soweputthoseerroroutoutto/dev/nullif[[$(netstat-anop2>/dev/null|grep$SERVICEPORT|grepLISTEN)||-f$PIDFILE]];then#pid=$(cat$PIDFILE)pid=$(ps-ef|grepjava|grep$BASENAME|grep-vgrep|awk'{print$2}’)if[[$pid!=””&&$(ps-ef|grep$pid|grep-vgrep)]];thenecho”SUCCESS:ChatterServerisOK”exit0elseecho”ERROR:ChatterServerpidisNOTexist”exit1fielif[[!$(netstat-anop2>/dev/null|grep$SERVICEPORT|grepLISTEN)]];thenecho”ERROR:ChatterServerportisNOTlisten”exit1elif[[!-f$PIDFILE]];thenecho”ERROR:ChatterServerpidfileisNOTexist”exit1elseecho”ERROR:ChatterServerisNOTrunning”exit1fi}start(){if[[-e$PIDFILE]];thenecho”ERROR:pidfile$PIDFILEexist,serverhasstartedwithpid$(cat$PIDFILE)”#pidfilecanbedeleted/bin/rm-f$PIDFILEexit1fiif[[-e$SERVER]];thenecho”INFO:StartingChatterServer”#StartChatterServercoredaemon#Whyusing”date+”%Y%m%d””?Becausewejustneedrestartthisonceperday#ForChatterServerwiilfindsomefilein$BASEDIRcd$BASEDIR#nohupjava-jar$SERVER$ARGS>>$LOGDIR/console-$(date+”%Y%m%d”).out2>&1&java-jar$SERVER$ARGS>>$LOGDIR/console-$(date+”%Y%m%d”).out2>&1&#java-jar$SERVER$ARGS>$LOGDIR/console.out2>&1&RETVAL=$?#shelldoNOTneedhomedirectory##ForChatterServerwiilfindsomefilein$BASEDIR#cdif[[$RETVAL-eq0]];then##$!–>ExpandstotheprocessIDofthemostrecentlyexecutedbackground(asynchronous)command.#echo$!>$PIDFILE#Forjavaperformanceissue,port29092willlistenlatter,wewillwaitingfor2secondsleep2#getpidvar#TODOremovedebuginfo#echo”DEBUG:”#ps-ef|grep$BASENAME|grep-vgrep|awk'{print$2}’#enddebugpid=$(ps-ef|grepjava|grep$BASENAME|grep-vgrep|awk'{print$2}’)#sendpidnumbertopidfileecho$pid>$PIDFILE#Thoselineswillremoveinnextrelease#TODOremovedebuginfo#echo”DEBUG:live1″#Forjavaperformanceissue,port29092willlistenlatter,sowechangejudgmentconditionsif[[$(netstat-anop2>/dev/null|grep$SERVICEPORT|grepLISTEN)||-f$PIDFILE]];thenecho”SUCCESS:ChatterServerstartOK”#Settingupstartlogecho”[$(date+”%D%T”)]SUCCESS:ChatterServerstartedwithpid$(cat$PIDFILE)”>>$LOGDIR/service.logfi#TODOremovedebuginfo#echo”DEBUG:live2″#-Thoselineswillremoveinnextrelease#echo”SUCCESS:ChatterServerstartOK”##Settingupstartlog#echo”[$(date+”%D%T”)]SUCCESS:ChatterServerstartedwithpid$(cat$PIDFILE)”>>$LOGDIR/service.logelseecho”ERROR:ChatterServerstartfailed”#Settingupstartlogecho”[$(date+”%D%T”)]ERROR:ChatterServerstartfailed”>>$LOGDIR/service.logexit$RETVALfielseecho”ERROR:Couldn’tfind$SERVER”#TODOWejustthinkthisisnotessential#DoNOTsettinguploghereexit1fi}stop(){if[[-e$PIDFILE]];thenpid=$(cat$PIDFILE)#ifkill-TERM$PIDFILE>/dev/null2>&1#TODOremovedebuginfo#echo”DEBUG:$LOGDIR/console-$(date+”%Y%m%d”).out”#UbuntucanNOTuse”usleep”,souse”sleep”instead#usleep100000ifkill-TERM$pid>>$LOGDIR/console-$(date+”%Y%m%d”).out&&sleep1thenecho”SUCCESS:ChatterServerstopOKwithTERM”#Settingupstoplogecho”[$(date+”%D%T”)]SUCCESS:ChatterServerstopOKwithTERM”>>$LOGDIR/service.log#BecausewecanNOTuseusleep,sowemustcommentoutsleep1next#sleep1#UbuntucanNOTuse”usleep”,souse”sleep”instead#usleep100000elifkill-KILL$pid>/dev/null2>&1&&sleep1thenecho”SUCCESS:ChatterServerstopOKwithKILL”#Settingupstoplogecho”[$(date+”%D%T”)]SUCCESS:ChatterServerstopOKwithKILL”>>$LOGDIR/service.log#BecausewecanNOTuseusleep,sowemustcommentoutsleep1next#sleep1elseecho”ERROR:ChatterServerstopfaild”#Settingupstoplogecho”[$(date+”%D%T”)]ERROR:ChatterServerstopfailed”>>$LOGDIR/service.logexit1fi#Removepidfileif[[-f$PIDFILE]];then/bin/rm-f$PIDFILEfielseecho”ERROR:NoChatterServerrunning”#TODOWejustthinkthisisnotessential#DoNOTsettinguploghereexit1fi}restart(){echo”INFO:RestartingChatterServer”stop#Thoselineswillremoveinnextreleaseif[[$(netstat-anop2>/dev/null|grep$SERVICEPORT|grepLISTEN)]];thenecho”WARNNING:port$SERVICEPORTisinusing,mustwaiting”sleep5if[[$(netstat-anop2>/dev/null|grep$SERVICEPORT|grepLISTEN)]];thenecho”WARNNING:port$SERVICEPORTisstillinusing,mustwaiting”sleep2fifi#-Thoselineswillremoveinnextrelease#DoNOTusingsleepanysecondsherewithstop()functionusedstart}case$1instatus)status;;start)start;;stop)stop;;restart)restart;;help|*)echo”Usage:$0{status|start|stop|restart|help}with$0itself”echo”Usage:servicechatter{status|start|stop|restart|help}withservice”exit1;;esac#replace”exit0″with”:”#exit0:

–end–

====================================分割线======================================

运维经验分享作为一个专题,目前共7篇文章

《运维经验分享(一)– Linux Shell之ChatterServer服务控制脚本》

《运维经验分享(二)– Linux Shell之ChatterServer服务控制脚本二次优化》

《运维经验分享(三)– 解决Ubuntu下crontab不能正确执行Shell脚本的问题(一)》

《运维经验分享(四)–关于 java进程管理的服务控制脚本编程思路分析》

《运维经验分享(五)– 改进的java进程管理的服务控制脚本》

《运维经验分享(六)– 深究crontab不能正确执行Shell脚本的问题(二)》

《运维经验分享(七)– Linux Shell之ChatterServer服务控制脚本第三次优化》

一个人负心,或许是因为他的记忆力不好。

运维经验分享(一)– Linux Shell之ChatterServer服务控制脚本

相关文章:

你感兴趣的文章:

标签云: