Bryce Bryce - 1 month ago 20
Python Question

Python - subprocess - Process not starting

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

Answer

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