我正在尝试自动化Linux 代理机器的启动过程。我需要运行位于两个不同目录中的两个可执行文件并将控件返回到提示符,以便继续执行其他一些任务,例如执行 grep 以检查这两个进程是否仍在运行。以下是我尝试使用 Python 的两种不同方法:
代码片段1:(通过分叉一个子进程)
import os
import pdb
def child():
cwd = os.getcwd()
os.chdir("THoT")
os.chdir("TH_Node")
print "Executing TH_Node.exe........."
command = "mono TH_Node.exe"
os.system(command)
os._exit(0)
def parent():
i = 0
while i < 1:
i = i + 1
newpid = os.fork()
if newpid == 0:
child()
else:
cwd = os.getcwd()
os.chdir("THoT")
os.chdir("TH_Protocol")
print "Executing TH_Protocol.exe........."
command1 = "mono TH_Protocol.exe"
os.system(command1)
parent()
代码片段2:(使用多处理)
import multiprocessing
import time
import sys
import os
import pdb
def TH_Protocol():
os.chdir("THoT")
os.chdir("TH_Protocol")
command = "mono TH_Protocol.exe"
os.system(command)
def TH_Node():
os.chdir("THoT")
os.chdir("TH_Node")
command1 = "mono TH_Node.exe"
os.system(command1)
if __name__ == '__main__':
d = multiprocessing.Process(name='TH_Protocol', target=TH_Protocol)
d.TH_Protocol = True
n = multiprocessing.Process(name='TH_Node', target=TH_Node)
n.TH_Protocol = False
d.start()
n.start()
d.join(1)
n.join()
问题是虽然我让进程 TH_Protocol.exe 和 TH_Node.exe 都运行,但我需要 ssh 到另一个会话以运行 grep 命令来检查这两个进程是否正在运行。我需要在与运行 python 脚本的会话相同的会话中恢复控制。我也尝试使用 subprocess.Popen ,但我面临同样的问题。有什么办法可以解决这个问题吗?