mongnodb 启动脚本

开始用mongodb建立一套监控体系,安装解压即可。附上编写的mongodb启动管理脚本。

建议 mkdir sbin 目录,放到sbin目录下。废话少说,代码如下:

#!/bin/bash



MONGODIR=$HOME/local/mongodb

MONGOD=$MONGODIR/bin/mongod

DBDIR=$MONGODIR/data/db

LOGPATH=$MONGODIR/log/mongodb.log



prog=$(basename $MONGOD)



# Get a sane screen width

[ -z "${COLUMNS:-}" ] && COLUMNS=80



if [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" ] ; then

  . /etc/sysconfig/i18n

  if [ "$CONSOLETYPE" != "pty" ]; then

    case "${LANG:-}" in

        ja_JP*|ko_KR*|zh_CN*|zh_TW*)

            export LC_MESSAGES=en_US

            ;;

        *)

            export LANG

            ;;

    esac

  else

    export LANG

  fi

fi



# Read in our configuration

if [ -z "${BOOTUP:-}" ]; then

  if [ -f /etc/sysconfig/init ]; then

      . /etc/sysconfig/init

  else

    # This all seem confusing? Look in /etc/sysconfig/init,

    # or in /usr/doc/initscripts-*/sysconfig.txt

    BOOTUP=color

    RES_COL=60

    MOVE_TO_COL="echo -en \\033[${RES_COL}G"

    SETCOLOR_SUCCESS="echo -en \\033[1;32m"

    SETCOLOR_FAILURE="echo -en \\033[1;31m"

    SETCOLOR_WARNING="echo -en \\033[1;33m"

    SETCOLOR_NORMAL="echo -en \\033[0;39m"

    LOGLEVEL=1

  fi

  if [ "$CONSOLETYPE" = "serial" ]; then

      BOOTUP=serial

      MOVE_TO_COL=

      SETCOLOR_SUCCESS=

      SETCOLOR_FAILURE=

      SETCOLOR_WARNING=

      SETCOLOR_NORMAL=

  fi

fi



if [ "${BOOTUP:-}" != "verbose" ]; then

   INITLOG_ARGS="-q"

else

   INITLOG_ARGS=

fi





# Check if $pid (could be plural) are running

checkpid() {

    local i



    for i in $* ; do

        [ -d "/proc/$i" ] || return 1

    done

    return 0

}

echo_success() {

  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL

  echo -n "[  "

  [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS

  echo -n $"OK"

  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

  echo -n "  ]"

  echo -ne "\r"

  if [ "$BOOTUP" = "graphical" -a -x /usr/bin/rhgb-client ]; then

    /usr/bin/rhgb-client -ping

  fi

  return 0

}



echo_failure() {

  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL

  echo -n "["

  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE

  echo -n $"FAILED"

  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

  echo -n "]"

  echo -ne "\r"

  if [ "$BOOTUP" = "graphical" -a -x /usr/bin/rhgb-client ]; then

    /usr/bin/rhgb-client -ping

  fi

  return 1

}

echo_running() {

  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL

  echo -n "["

  [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS

  echo -n $"Running"

  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

  echo -n "]"

  echo -ne "\r"

  if [ "$BOOTUP" = "graphical" -a -x /usr/bin/rhgb-client ]; then

    /usr/bin/rhgb-client -ping

  fi

  return 0

}





echo_stop() {

  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL

  echo -n "["

  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING

  echo -n $"Stopped"

  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

  echo -n "]"

  echo -ne "\r"

  if [ "$BOOTUP" = "graphical" -a -x /usr/bin/rhgb-client ]; then

    /usr/bin/rhgb-client -ping

  fi

  return 1

}





echo_warning() {

  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL

  echo -n "["

  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING

  echo -n $"WARNING"

  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

  echo -n "]"

  echo -ne "\r"

  if [ "$BOOTUP" = "graphical" -a -x /usr/bin/rhgb-client ]; then

    /usr/bin/rhgb-client -ping

  fi

  return 1

}



OLD_LD_LIBRARY_PATH=$LD_LIBRARY_PATH

export LD_LIBRARY_PATH=$(pwd)/lib



unset APP

if test -x $MONGOD

then

    APP=$MONGOD

fi



test "$APP" || { echo $MONGOD" not exists or not executable"; echo_failure; echo;exit 0; } 



app_pid_file=$MONGODIR/"$prog"_$(whoami).pid



start_app()

{

    if [ -e $app_pid_file ]; then

         ppid=$(cat $app_pid_file)

         psreault=`ps aux|grep $ppid|grep -v grep|grep $prog`

         if [ "$psreault"x !=  ""x ]; then

            echo -e "Warning: server is running, or you want restart?   \c"

            echo_warning

            echo

            exit 1

        fi

    fi

    echo  -e "Starting app ......      \c"

    $APP --dbpath $DBDIR --logpath $LOGPATH --logappend >/dev/null 2>&1 &

    #echo $!

    echo $! > $app_pid_file 

    sleep 1



    ppid=$!

    psreault=`ps aux|grep $ppid|grep -v grep|grep $prog`

    if [ "$psreault"x !=  ""x ]; then

        echo_success

        echo

        exit 1

    fi

    echo_failure

    echo

    exit 1



}



stop_app()

{

    if [ -e $app_pid_file ]; then 

        echo  -e "Stopping service         \c"

        ppid=$(cat $app_pid_file)

        psreault=`ps aux|grep $ppid|grep -v grep|grep $prog`

        if [ "$psreault"x !=  ""x ]; then

            kill  $ppid

        fi

    fi 

    rm -f $app_pid_file

    echo_success

    echo

}

show_status()

{

    echo  -e "app status ......        \c"

    if [ -e $app_pid_file ]; then

         ppid=$(cat $app_pid_file)

         psreault=`ps aux|grep $ppid|grep -v grep|grep $prog`

         if [ "$psreault"x !=  ""x ]; then

            echo_running

            echo

            exit 1

        fi

     fi    

    echo_stop

    echo

}





case "$1" in

    start)

        start_app ;

        exit 0

        ;;



    stop)

        stop_app ; 

        exit 0

        ;;

    restart)

        stop_app; 

        sleep 1

        start_app ;

        exit 0

        ;;

    status)

        show_status;

        exit 1

        ;;

     *)

       echo $"Usage: $0 {start|stop|restart|status}"

       exit 1

       ;;



esac



export LD_LIBRARY_PATH=$OLD_LD_LIBRARY_PATH

 

你可能感兴趣的:(脚本)