Shell脚本-检查mongod服务器是否正在运行 [英] shell script - check mongod server is running

查看:251
本文介绍了Shell脚本-检查mongod服务器是否正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个shell脚本来执行一些mongo db动作:

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

例如mongo testdb --eval "db.dropDatabase()"

但是,如果mongod服务器未运行,我会得到:

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

MongoDB shell version: 2.0.4
connecting to: testdb
Tue May 14 04:33:58 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84

mongo中是否可以检查连接状态?最终我想要这样的东西:

Is there a way in mongo I can check the connection status? Eventually I want something like:

if(mongod is running):
    mongo testdb --eval "db.dropDatabase()"
else:
    echo "pls make sure your mongod is running"
    exit 1

推荐答案

您应该能够创建如下所示的bash脚本:

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

这篇关于Shell脚本-检查mongod服务器是否正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆