shell脚本 - 检查的mongod服务器正在运行(shell script - check mo

2019-09-03 03:13发布

我有一个shell脚本,做一些蒙戈DB操作:

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

但是,如果该mongod的服务器没有运行,我得到:

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

是否有蒙戈我可以检查连接状态的方法吗? 最后,我想是这样的:

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

Answer 1:

你应该能够创建一个bash脚本是这样的:

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


Answer 2:

尝试在你的shell脚本运行以下命令: -

p纤ep的mongod

如果值是数字,你知道该进程正在运行,如果你得到一个空值,将其标记为服务没有运行?



Answer 3:

这就是我运行检查的mongod达:

# this script checks if the mongod is running.
# if not, send mail
#

EMAILIST=dba@wherever



`ps -A | grep -q '[m]ongod'`

if [ "$?" -eq "0" ]; then
    exit 0
else
    echo "The mongod server SB2MDB01 is DOWN" | mailx \
    -s "Server DOWN: SB2MDB01" $EMAILIST
fi

exit 0


Answer 4:

如果您有管理权限,你也可以做一个sudo systemctl status mongod它给你很多有用的附加信息:

mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2019-03-14 09:26:53 UTC; 5 weeks 9 days ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 420 (mongod)
   CGroup: /system.slice/mongod.service
           └─420 /usr/bin/mongod --config /etc/mongod.conf


文章来源: shell script - check mongod server is running
标签: mongodb shell