Popen a command that contains need to say yes for

2019-02-27 19:46发布

I need to automate the following command

cmd="yes | vgremove <vgname>"

whenever I code this command with

Popen(cmd.split(),stdout=PIPE,stderr=PIPE)

it does not complete. I suspect it waits till the command gets complete, so the pipe is struck, is there an alternative for this???

2条回答
Ridiculous、
2楼-- · 2019-02-27 20:08

There is a much easier way in this case:

Popen('vgremove -f <vgname>')

As for your question specifically:

p = Popen(cmd.split(), stdout=PIPE, stderr=PIPE, stdin=PIPE)
p.stdin.write('yes')
查看更多
forever°为你锁心
3楼-- · 2019-02-27 20:10

Piping is a shell feature, so you'll need shell=True on that. What you're doing without shell=True is executing yes with arguments. yes never stops executing so the subprocess never returns.

查看更多
登录 后发表回答