#/bin/bash ######################################################################################### ################ service info ########################################### ######################################################################################### ALIAS[0]="ls"; ALIAS[1]="eds"; ALIAS[2]="ccs"; ALIAS[3]="dbs"; ALIAS[4]="aas"; ALIAS[5]="tas"; ALIAS[6]="ctl"; ALIAS[7]="was"; ALIAS[8]="rcs"; ALIAS[9]="mcs"; ALIAS[10]="grs"; ALIAS[11]="vps"; ALIAS[12]="bp"; ALIAS[13]="ss"; ALIAS[14]="mnt"; PROC[0]="ls"; PROC[1]="eds"; PROC[2]="ccs"; PROC[3]="dbs"; PROC[4]="aas"; PROC[5]="tas"; PROC[6]="ctl"; PROC[7]="was"; PROC[8]="rcs"; PROC[9]="mcs"; PROC[10]="grs"; PROC[11]="vps"; PROC[12]="bp"; PROC[13]="ss"; PROC[14]="mnt"; DIR[0]="./"; DIR[1]="./"; DIR[2]="./"; DIR[3]="./"; DIR[4]="./"; DIR[5]="./"; DIR[6]="./"; DIR[7]="./"; DIR[8]="./"; DIR[9]="./"; DIR[10]="./"; DIR[11]="./"; DIR[12]="./"; DIR[13]="./"; DIR[14]="./"; ARGV[0]=""; ARGV[1]=""; ARGV[2]=""; ARGV[3]=""; ARGV[4]=""; ARGV[5]=""; ARGV[6]=""; ARGV[7]=""; ARGV[8]=""; ARGV[9]=""; ARGV[10]=""; ARGV[11]=""; ARGV[12]=""; ARGV[13]=""; ARGV[14]=""; PROC_COUNT=15 CHECK_TIME=10 MNT_INDEX=14 ######################################################################################### ################################################################################33 usage() { echo "Usage: start.sh [options] service1 [,service2..]"; echo "Start/stop services or show files version" echo " -c Get coredump, used with -k." echo " -d Run a daemon script for service" echo " -f Force kill service, used with -k" echo " -h Print help infomation" echo " -k Kill service" echo " -o Get owner authority [ -o user ]" echo " -r Restart service " echo " -s Show service status" echo " -v Show file version" echo " list Show all services status" echo " Note: If no option ,default to start a service. " echo "" echo "Valid shortcut for services:" echo " aas bp ccs ctl dbs eds grs ls mcs rcs ss tas vps was" echo " " echo "Example: " echo " \"start.sh list\" View status of all services." echo " \"start.sh aas\" Start agentserver." echo " \"start.sh -d aas\" Start agentserver with a daemon." echo " \"start.sh mcs aas\" Start mcserver and agentServer." echo " \"start.sh -v aas\" Show agentserver version." echo " \"start.sh -s aas\" Show agentserver status." echo " \"start.sh -k aas\" Stop agentserver." echo " \"start.sh -r aas\" Restart agentserver." echo " \"start.sh -kd aas\" Stop agentserver and its daemon." echo " \"start.sh -kf aas\" Force,kill agentserver immediately." echo " \"start.sh all\" Start all services at once." echo " \"start.sh -k all\" Stop all services at once." } echo_w() { width=`expr 60 - $1` f=`printf "%ds" $width` printf "%$f\n" "$2" } set_ulimit() { core_limit=`ulimit -c` [ $core_limit="0" ] if [ $? -eq "0" ]; then ulimit -c unlimited #echo "ulimit set" fi; } checkDaemonRunning() { ret=`ps -u $WHO -o pid -o comm -o cmd|grep "$SCRIPT_NAME -a -d $1"|grep -v $1[0-9a-zA-Z]|grep -v grep|awk '{print $1}'`; echo $ret; if [ "$ret" ]; then return 1; else return 0; fi } checkRunning() { ret=`ps -u $WHO -o pid -o comm|grep $1|grep -v $1[0-9a-zA-Z]|awk '{print $1}'`; echo $ret; if [ "$ret" ]; then return 1; else return 0; fi } show_status() { proc=${PROC[$1]} ret=`checkRunning $proc`; if [ $? -ne 0 ];then str="$proc (pid $ret)"; len=`expr length "$str"`; echo -n $str ret=`checkDaemonRunning ${ALIAS[$1]}`; if [ $? -ne 0 ];then echo_w `expr $len - 10` "[daemon][running]"; else echo_w `expr $len - 10` "[running]"; fi else echo -n $proc len=`expr length "$proc"`; echo_w `expr $len - 10` "[stopped]"; fi } restart_proc() { kill_proc $1; # [ $? -ne 0 ]&& return; start_proc $1; } kill_daemon() { len=`expr length "${PROC[$1]} daemon"` ret=`checkDaemonRunning ${ALIAS[$1]}`; if [ $? -ne 0 ];then echo -n "stoping ${PROC[$1]} daemon ."; for pid in "$ret" do kill -9 $pid; done echo_w $len "[stopped]"; fi } kill_proc() { result=1; [ "$DAEMON" = "true" ] && kill_daemon $1; proc=${PROC[$1]} echo -n "stoping $proc ."; len=`expr length "$proc"` ret=`checkRunning $proc`; if [ $? -ne 0 ];then for pid in "$ret" do if [ "$FORCE" = "true" ];then kill -9 $pid; elif [ "$CORE" = "true" ];then kill -6 $pid; else kill -9 $pid; fi for((t=0; t<10; t++)) do echo -n "." len=`expr $len + 1` ret=`checkRunning $proc`; if [ $? -ne 0 ];then sleep 1; else echo_w $len "[stopped]"; result=0; break; fi if [ $t -eq 8 ];then echo_w $len "[running]"; KILLFAILED=true; fi done done else echo -n ".." echo_w `expr $len + 2` "[stopped]"; fi return $result; } start_daemon() { $SCRIPT_NAME -a -d ${ALIAS[$1]} >/dev/null 2>&1 & echo "starting daemon for ${PROC[$1]}... OK"; # ret=`checkDaemonRunning ${ALIAS[$1]}`; # if [ $? -ne 0 ];then # echo "already a instance running ..."; # else # $SCRIPT_NAME -a -d ${ALIAS[$1]} >/dev/null 2>&1 & # echo "starting daemon for ${PROC[$1]}... OK"; # fi } start_real_daemon() { while true do start_proc $1 sleep $CHECK_TIME done } start_proc() { proc=${PROC[$1]} echo -n "starting $proc "; len=`expr length "$proc"` ret=`checkRunning $proc`; if [ $? -ne 0 ];then echo -n ".." echo_w `expr $len + 2` "[FAILED]" echo "Error:$proc already have a instance (pid $ret)"; return 1 else cd ${DIR[$1]} nohup ./$proc ${ARGV[$1]} >/dev/null 2>&1 & cd - >> /dev/null 2>&1 for t in 1 2 3 do echo -n "." len=`expr $len + 1` sleep 1 done echo -n "." ret=`checkRunning $proc`; if [ $? -ne 0 ];then echo_w `expr $len + 1` "[ OK ]"; else echo_w `expr $len + 1` "[FAILED]"; fi fi } show_version() { echo "====================== ${PROC[$1]} Version Info ======================"; cd ${DIR[$1]} ./${PROC[$1]} --version cd .. } do_process() { if [ "$KILL" = "true" ];then kill_proc $1; return; fi if [ "$RESTART" = "true" ];then restart_proc $1; return; fi if [ "$VERSION" = "true" ];then show_version $1; fi if [ "$STATUS" = "true" ];then show_status $1; fi if [ "$START" = "true" ];then start_proc $1; fi if [ "$DAEMON" = "true" ] && [ "$EXPAND" != "true" ];then start_daemon $1 fi if [ "$DAEMON" = "true" ] && [ "$EXPAND" == "true" ];then start_real_daemon $1; fi } ##========================================================================================================= ##========================================================================================================= ##= ##========================================================================================================= export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`/lib:`pwd`/oracle export TNS_ADMIN=`pwd`/oracle KILL=false; VERSION=false; STATUS=false; START=true; FORCE=false; CORE=false; DAEMON=false; RESTART=false; EXPAND=false; WHO=`whoami`; OWN=`stat -c %U $0` KILLFAILED=false; SCRIPT_NAME=$0 while getopts :krvsahfcdo: OPTION do case $OPTION in a) EXPAND=true; START=false;; k) KILL=true; START=false;; v) START=false; VERSION=true;; s) START=false; STATUS=true;; f) FORCE=true;; r) START=false; RESTART=true;; c) START=false; CORE=true;; o) WHO=$OPTARG;; d) START=false; DAEMON=true;; h) usage; exit 0;; \?) echo "start.sh: invalid option" echo "Tyr \"start.sh -h\" for more infomation." exit;; esac done shift `expr $OPTIND - 1`; if [ "$#" = "0" ];then echo "start.sh: missing operand." echo "Try \"start.sh -h\" for more infomation." exit 1; fi if [ $OWN != $WHO ];then echo "start.sh:sorry [$WHO], the owner is [$OWN]." echo "Add option \"-o $OWN\" to ignore this." echo "Try \"start.sh -h\" for more infomation." exit 1; fi set_ulimit; for proc in "$@" do num=-1 proc=`tr A-Z a-z <<< $proc`; if [ "$proc" = "list" ];then for((i=0; i