一個還算可以用的啟動指令碼

LifeSecret發表於2017-01-07
#!/bin/sh
#
# as it does use of the /proc filesystem.

EXEC=/opt/123/mongo/bin/mongod
PIDFILE=/opt/123/mongo/mongo.pid
CONF=/opt/123/mongo/mongo.conf

start() {
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting mongo..."
                $EXEC --config=$CONF
        fi
}
stop() {
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                kill -HUP $PID
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for mongo to -shutdown ..."
                    sleep 1
                done
                rm -rf $PIDFILE
                echo "mongo stopped"
        fi
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

這個指令碼用於mongodb的啟動、關停、重啟操作。

重點:
1. start stop函式單獨拿出來,除了分別滿足start和stop之外,還可以方便的滿足restart的需求。

相關文章