首页 > 解决方案 > 使python程序`gnome-terminal`独立

问题描述

我编写了一个 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()

标签: pythonpython-2.7

解决方案


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

/data/mcp-8100/apps/elasticsearch/bin'

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

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

推荐阅读