From within a Python script, I'm launching an executable in a subprocess (for anyone interested: it's the terraria server).
# script.py
server_process = subprocess.Popen("my_executable arg1 arg2", stdin=subprocess.PIPE, text=True)
# My Bash shell
$ ps aux | grep my_executable
# I've removed everything but the PIDs and CLIs below
14701 my_executable arg1 arg2
my_executable starts writing to stdout as expected. However I can't seem to write to stdin from that same Python script
# script.py
server_process.stdin.write('exit\n')
server_process.stdin.flush()
my_executable terminates itself upon reading exit, so that should kill it: but nothing happens
Here's the wrinkle: I can write to my_executable's stdin via /proc from a separate process (like my bash shell) just fine!
# Bash shell
$ sudo echo exit > /proc/14701/fd/0 # Works
$ python3 -c 'import os; os.system("sudo echo exit > /proc/14701/fd/0")' # Also works!
Putting that os.system call in the original script with server_process.pid doesn't work though
# script.py
os.system(f"sudo echo exit > /proc/{server_process.pid}/fd/0")
# sh: 1: cannot create /proc/14701/fd/0: Directory nonexistent
It seems like writes to stdin from a separate process (like my bash shell) work, whereas writes from within the process (group?) don't. What's going on here?