【发布时间】:2012-10-18 13:49:29
【问题描述】:
我想从 python 脚本中暂停 bash 脚本,步骤如下:
我从 python script reader.py 启动 script writer.sh。
当 writer.sh 输出第三行时,我希望使用
reader.py中的某些命令暂停执行此脚本。我想使用
reader.py中的某个命令继续执行 writer.sh。
这是这两个脚本,问题是writer.sh 在reader.py 中执行睡眠命令时不会暂停。所以我的问题是,当writer.sh 输出字符串“third”时,如何暂停它?确切地说(这是我工作中的实际问题)我希望 writer.sh 停止,因为 reader.py 停止读取 writer.sh 的输出。
reader.py:
import subprocess
from time import sleep
print 'One line at a time:'
proc = subprocess.Popen('./writer.sh',
shell=False,
stdout=subprocess.PIPE,
)
try:
for i in range(4):
output = proc.stdout.readline()
print output.rstrip()
if i == 2:
print "sleeping"
sleep(200000000000000)
except KeyboardInterrupt:
remainder = proc.communicate()[0]
print "remainder:"
print remainder
writer.sh:
#!/bin/sh
echo first;
echo second;
echo third;
echo fourth;
echo fith;
touch end_file;
相关问题是,如果 script1 输出文本行,是否会在 Linux 上使用管道暂停 script1,例如script1 | script2,
并且 script2 在读取第三行输入后暂停?
【问题讨论】:
标签: linux subprocess