gpt4 book ai didi

python - python 子进程中的 watch 命令仅打印输出中的更改

转载 作者:太空宇宙 更新时间:2023-11-03 19:53:26 27 4
gpt4 key购买 nike

我确实使用 python subprocess 模块来获取 kubernetes 应用程序的状态,如下所示:

command = gravity status | grep -o 'Cluster status:.*' | cut -f2- -d:

def run_cmd(command):
process = subprocess.Popen(command,shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
return out, err;

out: active

确实生成输出“active”或“degraded”,但我想实现一个“watch”,因此仅当将“active”更改为“degraded”时才会打印'degraded' to 'active' 不要每隔几分钟运行一次来​​向我提供状态。

Question: how I can do this in python as a script or it is possible ?

最佳答案

试试这个方法

a=1
while a==1:
out = subprocess.Popen('date',
stdout=subprocess.PIPE).communicate()[0]
time.sleep(2)
out2 = subprocess.Popen('date',
stdout=subprocess.PIPE).communicate()[0]
if out != out2:
print (out)

关于python - python 子进程中的 watch 命令仅打印输出中的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59688246/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com