我想从bash命令实时输出的,所以我可以与数据更容易发挥。子没有返回,直到命令完成
在此代码,iostat 1
工作正常,并以1秒的打印输出的命令。命令sar -u 1 20
,它运行在命令行(打印1行每秒最多20个)上按预期,等待直到命令完成〜20秒,印刷用0.1秒延迟每一行之前。
我打算无限期地运行这些命令,并且需要这部分工作。任何想法什么是错的?我在OSX上。
import subprocess
import time
# run a command and constantly check for new output, if you find it, print it and continue
# if the command is done, break out
try:
command="sar -u 1 20"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
while True:
time.sleep(.1) # so i don't kill the cpu on the machine i'm checking
output = process.stdout.readline()
#if process.poll() is not None:
# break
if output:
print output.strip()
except KeyboardInterrupt:
print 'Exiting...'
return_code = process.poll()
print return_code
这看起来像我的问题是,我似乎无法安装它,但它应该通过coreutils安装。我找到了描述这个工具的http://apple.stackexchange.com/questions/193141/to-use-stdbuf-from-homebrews-coreutils。 – user1601716
后'冲泡安装coreutils',你需要使用'gstdbuf';该软件包将所有命令名加上'g'作为前缀以避免与标准工具发生冲突('gls'而不是'ls'等''stdbuf'不会发生冲突,但显然打包者希望保持一致。) – chepner
有安装coreutils的问题,看起来像一个错误'make install',有一个无限名称的文件:(但这看起来是问题,我现在可以解决这个问题。谢谢! – user1601716