Identifying mongo server status from bash

1.4k views Asked by At

I am writing a bash script in our linux server about extracting a report from MongoDB. In fact, there are two more replica servers and I should extract the report if the it is master at that time for that server. If not, script will not extract anything.

Is there a way to get that information from shell? Thanks.

1

There are 1 answers

1
David Modica On BEST ANSWER

you have to run a command to find out if it the PRIMARY or not.

i have a "js" file which contains this:

`printjson(db.isMaster().ismaster);

then in my bash script i run execute this and act accordingly:

PRIMARY=`/usr/bin/mongo ${SERVERNAME}:${PORT} --quiet     ${SCRIPTDIR}/isMaster.js`;
#
#
if [ "$PRIMARY" != "false" ]; then
# it is the PRIMARY

cheers