Python 检查第二个脚本是否正在运行或已完成

Python 检查第二个脚本是否正在运行或已完成,python,linux,process,tmux,Python,Linux,Process,Tmux,我需要在scriptB.py中检查ascriptA.py是否仍在运行。两者都是单独启动的,但只有在scriptA.py仍在运行时,scriptB.py才能继续 我知道我可以用 import subprocess process = subprocess.Popen(['pgrep', 'scriptA.py'], stdout=subprocess.PIPE) process.wait() if not process.returncode: print "Process runn

我需要在
scriptB.py
中检查a
scriptA.py
是否仍在运行。两者都是单独启动的,但只有在
scriptA.py
仍在运行时,
scriptB.py
才能继续

我知道我可以用

import subprocess

process = subprocess.Popen(['pgrep', 'scriptA.py'], stdout=subprocess.PIPE)

process.wait()

if not process.returncode:
    print "Process running"
else:
    print "Process not running"
但是脚本a在tmux会话中运行。它被称为like
tmux new-d-s scriptA_2014-12-09-10-54-02-697559'cd/home/user/scriptA;python scriptA.py-flag;echo$?>/tmp/scriptA_2014-12-09-10-54-02-697559'

如果我
pgrep scriptA.py
它不会返回
PID
pgreptmux
可以工作,但是可能还有其他tmux会话,所以我不能使用它

我可以做一些类似于
ps aux | grep scriptA.py | wc-l
的事情,并检查行数-但这感觉非常多变


还有什么方法可以验证
scriptA.py
是否正在运行?

我现在正在使用
PID
,在脚本执行时写入文件。。我使用的代码似乎适用于我的案例:

scriptA
中,执行开始时:

pidf = open("./scriptA_pid.tmp","w")
pidf.write(str(os.getpid()))
pidf.close()
scriptB
中,在需要执行的循环开始处,直到
scriptA
完成

with open("./scriptA_pid.tmp","r") as f:
    scriptA_pid = f.read()
chk_sA = subprocess.Popen(['kill -0 '+str(scriptA_pid)+' > /dev/null 2>&1; echo $?'],stdout=subprocess.PIPE,stderr=devnull,shell=True)
chk_sA.wait()
sA_status = chk_sA.stdout.read()

if int(sA_status) == 0:
    #ScriptA is still running
    pass
else:
    #ScriptA is not running
    sys.exit(0) 

您可以在一个单独的scriptA线程中实现一个简单的套接字服务器,并从scriptB ping它以查看它是否应答……这是可能的,但这感觉像是一种过度使用。我还考虑过在脚本开始时将
PID
写入一个文件,在第二个文件中读取它。PID文件似乎是这里最简单的修复方法,其他任何东西,您可能会看到套接字级别或类似的东西