Jacob Birkett
Jacob Birkett

Reputation: 2125

Send commands to subprocess.Popen() process

I need to pass commands to a process on subprocess.Popen() but when I do, it only works if I use stdin.close() afterwards. My code is below.

sprocess.stdin.write('/stop'.encode())
sprocess.stdin.flush()
sprocess.stdin.close()
sprocess.stdout.close()

Works, but that is because of stdin.close() and I need to be able to do this without closing the pipes.

How can I pass commands to the process without closing the pipes after?

Upvotes: 4

Views: 10667

Answers (1)

MisterMiyagi
MisterMiyagi

Reputation: 50066

Note that sprocess.stdin.write('/stop'.encode()) will not add a newline! Your subprocess is probably expecting a line of input, signified by either a newline or EOF. Closing stdin will send that EOF.

Try sending sprocess.stdin.write('/stop\n'.encode()) instead.

Upvotes: 5

Related Questions