3
是否可以在shell中使用watch命令来处理子进程?使用'watch'命令的子进程
tail = subprocess.Popen("watch -n 1 'tail -n 1 /mnt/syslog/**/*.log | grep :'", shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
因为它不为我工作...
是否可以在shell中使用watch命令来处理子进程?使用'watch'命令的子进程
tail = subprocess.Popen("watch -n 1 'tail -n 1 /mnt/syslog/**/*.log | grep :'", shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
因为它不为我工作...
你的子命令是正确的,但你指定输出和错误缓冲之后,你需要从他们阅读。这是做到这一点的一种方法:
1 #!/usr/env/python
2
3 import subprocess
4 import sys
5
6 cmd = ['watch', '-d', 'tail', '-n', '1', '/var/log/messages']
7
8 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
9
10 for line in iter(lambda: p.stdout.read(1), ''):
11 sys.stdout.write(line)
12 sys.stdout.flush()
13
我已经声明了该命令作为列表参数。这是子流程模块推荐的做法。然后for循环迭代缓冲区,一次读取1个字节并将其打印到屏幕上。一旦子流程死亡或关闭它,此流将被关闭。
您是否尝试过使用'os.system(your_cmd)'来运行您的命令? – Aditya
什么不起作用?发生什么事? – JohanL