我已经写了一个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()发布于 2018-05-15 03:25:58
您管理路径的方式是特定于unix的:
/data/mcp-8100/apps/elasticsearch/bin‘
如果你想独立于平台,你需要使用操作系统原语:
os.path.join('data','mcp-8100','apps','elasticsearch','bin')https://stackoverflow.com/questions/50337605
复制相似问题