我想從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