Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 446
Jacob Birkett Avatar asked Oct 19 '25 16:10

Jacob Birkett


1 Answers

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.

like image 80
MisterMiyagi Avatar answered Oct 21 '25 05:10

MisterMiyagi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!