Determine if a subprocess reads from stdin

Viewed 53

I have a python script that starts other processes via subprocess.run(...). Usually such a process runs automatically without reading from stdin. However, in the case of an error the processes waits for user input. I want detect such a situation from the calling python script and then e.g. kill that process. How could I achieve this?

(My idea was to create a file-like object which raises an Exception when its .read() method is called and pass it to subprocess.Popen(...) as a "bait" for the stdin kwarg. However this seems somehow ugly because some fileno needed to be faked.)

1 Answers

You can just pass an empty file, e.g. subprocess.DEVNULL. A process which waits for standard input will get an EOF signalled if it tries reading from such a file.

Related