1

EDIT: So far, this does not appear to be an issue with mongod.lock as others have suggested, as I am wiping out the contents of /opt/databases/db directory each run, manually.

I have a simple script that checks to see if my mongod process is running as well as Apache Activemq. If they are both running, the script exits. Otherwise, it will attempt to start one or both processes.

However, currently the script goes through the motions of starting activemq and mongod but for some reason they do not stay alive. Any ideas?

My code looks as follows:

def checkMongo():
    try:
        client = pymongo.MongoClient("localhost:27017", serverSelectionTimeoutMS=5)
        client.server_info()
        return True
    except pymongo.errors.ServerSelectionTimeoutError as err:
        print err
        return False
def checkActivemq():
    args = ['/opt/activemq/bin/activemq', 'status']
    try :
        proc = subprocess.check_output(args)
        print proc
        if 'ActiveMQ is running (pid ' in proc:
            return True
    except subprocess.CalledProcessError as e:
        return False

if checkMongo():
    print "Mongod is running"
else:
    print "Mongod not running. Attempting to start Mongod"
    subprocess.Popen(["mongod", "--fork", "--logpath /opt/logs/mongod.log", "--dbpath=/opt/databases/db" ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    time.sleep(5)
    print "Checking to see if mongod started"
    if checkMongo():
        print "Mongo successfully started."
    else:
        print "FATAL: Mongod unable to start. :("
        exit
print "Now activating activemq"
if checkActivemq():
    print "Woot activemq is running"
else:
    print "Activemq is not running. Starting activemq"
    subprocess.Popen(['/opt/activemq/bin/activemq', 'start'])
    time.sleep(5)
    if checkActivemq():
        print "activemq started succesfully."
    else:
        print "FATAL: Activemq did not start succesfully"

Output received:

Checking if mongod is up
localhost:27017: [Errno 111] Connection refused
Mongod not running. Attempting to start Mongod
Checking to see if mongod started
localhost:27017: [Errno 111] Connection refused
FATAL: Mongod unable to start :(
Now activating activemq
Activemq is not running. Starting activemq
INFO: Loading '/opt/apache-activemq-5.14.1//bin/env'
INFO: Using java '/usr/bin/java'
INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get details
INFO: pidfile created : '/opt/apache-activemq-5.14.1//data/activemq.pid' (pid '39632')
FATAL: Activemq did not start succesfully
Bryce
  • 383
  • 4
  • 19
  • [This](http://stackoverflow.com/questions/6857781/what-is-the-use-of-the-mongo-lock-file) may also help – Aaron Oct 25 '16 at 19:08
  • I dont have any files in /opt/databases/db/ so neither of those is applicable, unfortunately. Furthermore when I run from command line: mongod --fork --logpath /opt/logs/mongod.log --dbpath=/opt/databases/db mongod starts just fine. Any ideas? – Bryce Oct 25 '16 at 19:34
  • 1
    one question... why are you using `--fork`? [this](https://docs.mongodb.com/v3.2/reference/configuration-options/#processManagement.fork) says the linux init scripts don't like --fork, and you should use the calling program's faculties to call as a background process (ie popen). It seems you're creating a background process in a background process.. – Aaron Oct 26 '16 at 13:29
  • also, [others](http://stackoverflow.com/q/24899849/3220135) seem to have this problem when your ip tables are configured incorrectly – Aaron Oct 26 '16 at 13:43
  • 1
    @Aaron's answer was correct. Thanks again all! – Bryce Nov 04 '16 at 18:46

1 Answers1

1

See @Aaron's suggestion. Subprocess within subprocess = no good.

Bryce
  • 383
  • 4
  • 19