python执行shell命令并继续而不等待,并在执行前检查是否正在运行

tra*_*lad 4 python shell command-line

我需要从另一个执行另外两个python脚本.命令看起来像这样:

#python send.py

#python wait.py

这将在一个循环中发生,该循环将休眠1分钟然后重新运行.

在执行命令以启动其他脚本之前,我需要确保它们仍然没有运行.

ami*_*che 11

您可以使用subprocess.Popen来执行此操作,例如:

import subprocess

command1 = subprocess.Popen(['command1', 'args1', 'arg2'])
command2 = subprocess.Popen(['command2', 'args1', 'arg2'])
Run Code Online (Sandbox Code Playgroud)

如果需要检索输出,请执行以下操作:

command1.wait()
print command1.stdout
Run Code Online (Sandbox Code Playgroud)

示例运行:

sleep = subprocess.Popen(['sleep', '60'])
sleep.wait()
print sleep.stdout  # sleep outputs nothing but...
print sleep.returncode  # you get the exit value
Run Code Online (Sandbox Code Playgroud)