使python程序`gnome-terminal`独立

时间:2018-05-14 19:22:38

标签: python python-2.7

我编写了一个python脚本,它启动不同程序保存在不同终端的不同文件夹中。每个程序一旦启动就不会结束,我必须启动四个程序。因此,我决定使用subprocess.call(['gnome-terminal', '-e', "gradle run"])在不同的终端中独立启动每个程序,而不是os.system('gradle run')启动程序(不会结束)。现在我想让这个程序gnome-terminal独立,以便它可以在其他操作系统上使用以及如何继续。 这是整个程序的代码:

#!/usr/bin/env python

import os
import socket
import urllib
import subprocess


path_apphome = os.path.dirname(os.path.abspath(__file__)) + '/..'
os.chdir(path_apphome)
# os.system('ls')

def checkportopen(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    return sock.connect_ex(('127.0.0.1', port)) == 0

def mkapps():
    if not os.path.isdir(path_apphome + '/data'): os.makedirs(path_apphome + '/data')
    if not os.path.isdir(path_apphome + '/data/mcp-8100'): os.makedirs(path_apphome + '/data/mcp-8100')
    if not os.path.isdir(path_apphome + '/data/mcp-8100/apps'): os.makedirs(path_apphome + '/data/mcp-8100/apps')

def run_mcp():
    subprocess.call(['gnome-terminal', '-e', "gradle run"])

def run_loader():
    os.system('cd ../yacy_grid_loader')
    subprocess.call(['gnome-terminal', '-e', "gradle run"])

def run_crawler():
    os.system('cd ../yacy_grid_crawler')
    subprocess.call(['gnome-terminal', '-e', "gradle run"])

def run_parser():
    os.system('cd ../yacy_grid_parser')
    subprocess.call(['gnome-terminal', '-e', "gradle run"])


if not checkportopen(9200):
    print "Elasticsearch is not running"
    mkapps()
    elasticversion = 'elasticsearch-5.6.5'
    if not os.path.isfile(path_apphome + '/data/mcp-8100/apps/' + elasticversion + '.tar.gz'):
        print('Downloading ' + elasticversion)
        urllib.urlretrieve ('https://artifacts.elastic.co/downloads/elasticsearch/' + elasticversion + '.tar.gz', path_apphome + '/data/mcp-8100/apps/' + elasticversion + '.tar.gz')
    if not os.path.isdir(path_apphome + '/data/mcp-8100/apps/elasticsearch'):
        print('Decompressing' + elasticversion)
        os.system('tar xfz ' + path_apphome + '/data/mcp-8100/apps/' + elasticversion + '.tar.gz -C ' + path_apphome + '/data/mcp-8100/apps/')
        os.rename(path_apphome + '/data/mcp-8100/apps/' + elasticversion, path_apphome + '/data/mcp-8100/apps/elasticsearch')
    # run elasticsearch
    print('Running Elasticsearch')
    os.chdir(path_apphome + '/data/mcp-8100/apps/elasticsearch/bin')
    os.system('nohup ./elasticsearch &')

os.chdir(path_apphome)

if checkportopen(15672):
    print "RabbitMQ is Running"
    print "If you have configured it according to YaCy setup press N"
    print "If you have not configured it according to YaCy setup or Do not know what to do press Y"
    n=raw_input()
    if(n=='Y' or n=='y'):
        os.system('service rabbitmq-server stop')

if not checkportopen(15672):
    print "rabbitmq is not running"
    os.system('python bin/start_rabbitmq.py')

subprocess.call('bin/update_all.sh')

if not checkportopen(2121):
    print "ftp server is not Running"

if not checkportopen(8100):
    print "yacy_grid_mcp is not running,running yacy_grid_mcp in new terminal"
    run_mcp()


if not checkportopen(8200):
    print "yacy_grid_loader is not running,running yacy_grid_loader in new terminal"
    run_loader()


if not checkportopen(8300):
    print "yacy_grid_crawler is not running,running yacy_grid_crawler in new terminal"
    run_crawler()


if not checkportopen(8500):
    print "yacy_grid_parser is not running,running yacy_grid_parser in new terminal"
    run_parser()

1 个答案:

答案 0 :(得分:0)

您管理路径的方式是特定于unix的:

  

/数据/ MCP-8100 /应用/ elasticsearch / bin'的

如果您想独立于平台,则需要使用OS原创:

os.path.join('data','mcp-8100','apps','elasticsearch','bin')