首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使python程序‘`gnome terminal`’独立

使python程序‘`gnome terminal`’独立
EN

Stack Overflow用户
提问于 2018-05-15 03:22:38
回答 1查看 103关注 0票数 0

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

代码语言:javascript
复制
#!/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()
EN

回答 1

Stack Overflow用户

发布于 2018-05-15 03:25:58

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

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

如果你想独立于平台,你需要使用操作系统原语:

代码语言:javascript
复制
os.path.join('data','mcp-8100','apps','elasticsearch','bin')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50337605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档