linux 服务管理
修改主要是针对restart功能
原来的restart,会在stop的时候,相关的服务并没有真正的停止,所以才进行了如下的修改。
#!/bin/sh # # *_app program # chkconfig: 345 86 31 # description: *_app is a essential part of * PID_PREFIX='/usr/local/*/app/run' BIN_PREFIX='/usr/local/*/app/sbin' SUPERVISOR_SERVER="/usr/local/*/app/engine/bin/supervisord" ; SUPERVISOR_CONFIG="/usr/local/*/app/conf/supervisord.ini" ; SUPERVISOR_PID="/usr/local/*/app/run/supervisord.pid" ; PROCESS_LIST="authenticator rulefilter receiver dispatcher sizequerier proxy_monitor" ########################################################### # Start operate start() { for name in $PROCESS_LIST; do # check already running PID_FILE="$PID_PREFIX/$name.pid" if [ -f $PID_FILE ]; then PID=`cat $PID_FILE` ; if ps ax | grep $name | grep -v 'grep' | grep $PID &> /dev/null; then echo "$name: started" ; continue ; fi fi # start program $BIN_PREFIX/$name start; if [ ! $? -eq 0 ]; then echo "$name: start failure" ; continue ; fi done return 0 ; } ########################################################### # Stop operate stop_supervisor() { # check supervisor already stop if [ ! -f $SUPERVISOR_PID ]; then return 0 ; fi PID=`cat $SUPERVISOR_PID` ; if ! ps ax | grep supervisord | grep -v 'grep' | grep $PID &> /dev/null; then return 0 ; fi # stop supervisor kill `cat $SUPERVISOR_PID` ; sleep 0.5 ; if [ -f $SUPERVISOR_PID ]; then return 1 ; else return 0 fi } check_app_stop() { _VAL=0 ; for name in $PROCESS_LIST; do PID_FILE="$PID_PREFIX/$name.pid"; # check pid file is exist if [ ! -f $PID_FILE ]; then echo "$name: stopped" ; continue ; fi # check pid is valid PID=`cat $PID_FILE` ; if ps ax | grep $name | grep -v 'grep' | grep $PID &> /dev/null; then echo "Error: app $name stop failure" ; _VAL=2 ; else echo "$name: stopped" ; fi done return $_VAL ; } stop() { # check supervisor already stop if [ ! -f $SUPERVISOR_PID ]; then echo "all stopped" ; return 0 ; fi # stop supervisor stop_supervisor ; if [ ! $? -eq 0 ]; then return 1 ; fi # check app stop check_app_stop ; return $? ; } ########################################################### # Restart operate restart() { stop; sleep 5; stop_supervisor; # check supervisor if [ -f $SUPERVISOR_PID ]; then if ps ax|grep 'supervisord'|grep -v 'grep' &>/dev/null; then kill `cat $SUPERVISOR_PID`; else rm -rf $SUPERVISOR_PID; fi else if ps ax|grep 'supervisord'|grep -v 'grep' &>/dev/null; then kill `ps ax|grep 'supervisord'|grep -v 'grep'|awk '{print $1}'` fi fi sleep 3; #kill pid process for pid in `ps ax|grep 'python'|grep -v 'grep'|grep '*/app/repo'|awk '{print $1}'`; do kill $pid done sleep 2; #clean pid file cd $PID_PREFIX if ls $PID_PREFIX/*.pid &> /dev/null; then rm -f $PID_PREFIX/*.pid fi start; return 0 ; } ########################################################### # Status operate status() { # check supervisor running if [ ! -f $SUPERVISOR_PID ]; then echo "all stopped" ; return 1 ; fi # check app program status _VAL=0 ; for name in $PROCESS_LIST; do $BIN_PREFIX/$name status; if [ ! $? -eq 0 ]; then _VAL=1 ; fi done return $_VAL ; } ########################################################### # call program _RET_VAL=0 case "$1" in start) start ; _RET_VAL=$? ; ;; stop) stop ; _RET_VAL=$? ; ;; restart) restart ; _RET_VAL=$? ; ;; status) status ; _RET_VAL=$? ; ;; *) echo "Usage: $basename {start|stop|restart|status}" ; _RET_VAL=1 ;; esac exit $_RET_VAL