我正在尝试编写一些Python代码,如果另一个Python代码没有启动,则会在子进程中启动它,否则终止终端和应用程序(Linux)。
所以它看起来像这样:
#!/usr/bin/python
from subprocess import Popen
text_file = open(".proc", "rb")
dat = text_file.read()
text_file.close()
def do(dat):
text_file = open(".proc", "w")
p = None
if dat == "x" :
p = Popen('python StripCore.py', shell=True)
text_file.write( str( p.pid ) )
else :
text_file.write( "x" )
p = # Assign process by pid / pid from int( dat )
p.terminate()
text_file.close()
do( dat )
我不知道如何通过进程ID为应用程序从文件“.proc”中读取名称。另一个问题是解释器说字符串“dat”不等于“x”? 我漏掉了什么?
shell=True
参数?据我所知,在你的使用情况下这是不必要的。另请注意,当使用shell=True
时,p.pid
返回的进程 ID 不是 Python 进程的 PID,而是生成用于执行该进程的 shell 的 PID。 - Bakuriu