shell script - check mongod server is running

前端 未结 4 838
既然无缘
既然无缘 2021-02-02 03:57

I have a shell script to do some mongo db actions:

e.g. mongo testdb --eval \"db.dropDatabase()\"

BUT, if the mongod server is not running, I get:

4条回答
  •  遥遥无期
    2021-02-02 04:24

    You should be able to create a bash script like this:

    mongo --eval "db.stats()"  # do a simple harmless command of some sort
    
    RESULT=$?   # returns 0 if mongo eval succeeds
    
    if [ $RESULT -ne 0 ]; then
        echo "mongodb not running"
        exit 1
    else
        echo "mongodb running!"
    fi
    

提交回复
热议问题