我有一個shell腳本來執行一些mongo數據庫操作:shell腳本 - 檢查mongod服務器是否正在運行
例如, 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
一個簡單的方法:你可以檢查'%ERRORLEVEL%'在Windows或bash'$?'中查看mongo命令是否返回錯誤代碼。 – WiredPrairie 2013-05-14 12:33:57
@WiredPrairie在bash中。 – Shengjie 2013-05-14 12:46:59