Python - 子进程 - 进程无法启动

时间:2016-10-25 19:04:02

标签: python subprocess

编辑:到目前为止,这似乎与其他人建议的mongod.lock不一样,因为我每次运行时都会清除/ opt / databases / db目录的内容,手动。

我有一个简单的脚本,可以检查我的mongod进程是否正在运行以及Apache Activemq。如果它们都在运行,则脚本退出。否则,它将尝试启动一个或两个进程。

然而,目前脚本经历了启动activemq和mongod的动作,但由于某种原因,它们不会活着。有任何想法吗?

我的代码如下:

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"

收到的输出:

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

1 个答案:

答案 0 :(得分:1)

参见@Aaron的建议。子流程中的子流程=没有好处。