shell script - check mongod server is running

前端 未结 4 837
既然无缘
既然无缘 2021-02-02 03:57

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

e.g. mongo testdb --eval \"db.dropDatabase()\"

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

相关标签:
4条回答
  • 2021-02-02 04:21

    try running this in your shell script:-

    pgrep mongod

    If the value is numeric you know that the process is running, if you get an empty value, flag it as service not running...

    0 讨论(0)
  • 2021-02-02 04:23

    this is what i run to check if mongod is up:

    # 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
    
    0 讨论(0)
  • 2021-02-02 04:24

    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
    
    0 讨论(0)
  • 2021-02-02 04:35

    If you have administrative rights, you could also do a sudo systemctl status mongod which gives you lots of helpful additional information:

    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
    
    0 讨论(0)
提交回复
热议问题