我在系统启动时从python启动另一个进程时遇到问题, 但不是从终端运行时。
我的代码:
import pika
import time
import sys
import logging
import json
import os
import config
import subprocess as sp
from pika.exceptions import AMQPConnectionError, ConnectionClosed
logging.basicConfig(filename='/opt/myapp/logs/pika.log', format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
global con
try:
con = pika.BlockingConnection(pika.ConnectionParameters(host=config.HOST_IP))
print "Connected to RabbitMQ Server..."
logging.info('Connected to RabbitMQ Server')
except AMQPConnectionError:
sys.exit(0)
ch = con.channel()
ch.queue_declare(queue='msgs', durable=True)
def on_recieve(ch, method, properties, body):
print body
data = json.loads(body)
res_ip_raw = data['laptop_ip']
res_ip_decoded = res_ip_raw.replace("\r\n", "")
if res_ip_decoded == config.LAPTOP_IP:
logging.info(body)
# **Unable to lauch this**
child=sp.Popen("/usr/bin/firefox", shell=True)
logging.info('Launching firefox with pid '+str(child.pid) )
time.sleep( body.count('.'))
ch.basic_ack( delivery_tag = method.delivery_tag)
ch.basic_qos(prefetch_count=1)
ch.basic_consume(on_recieve, queue='msgs')
if __name__ == '__main__':
try:
ch.start_consuming()
except (ConnectionClosed, KeyboardInterrupt, SystemExit):
sys.exit(0)
这是我的启动脚本
start_up.sh
#!/bin/sh
nohup /usr/bin/python /opt/myapp/main.py &
main.py工作正常,firefox启动 当我从终端运行这个时 ./main.py或/etc/init.d/start_up.sh
任何帮助?
谢谢!