Bryce Bryce - 1 year ago 111
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():
client = pymongo.MongoClient("localhost:27017", serverSelectionTimeoutMS=5)
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"
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)
print "Checking to see if mongod started"
if checkMongo():
print "Mongo successfully started."
print "FATAL: Mongod unable to start. :("
print "Now activating activemq"
if checkActivemq():
print "Woot activemq is running"
print "Activemq is not running. Starting activemq"
subprocess.Popen(['/opt/activemq/bin/activemq', 'start'])
if checkActivemq():
print "activemq started succesfully."
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 and to get details
INFO: pidfile created : '/opt/apache-activemq-5.14.1//data/' (pid '39632')
FATAL: Activemq did not start succesfully

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download