Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying mongo server status from bash

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.

like image 721
skynyrd Avatar asked Apr 21 '26 05:04

skynyrd


1 Answers

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

like image 93
David Modica Avatar answered Apr 22 '26 23:04

David Modica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!