I want to execute an arbitrary user-provided command in that user's $SHELL
. The command may be short-lived (like ls
) or long-lived (like firefox
), and it may be a single command, a pipeline, or any other shell-supported construct.
The method of execution must indicate success or failure on behalf of the shell and must not block regardless of when or whether the command terminates. The command may never terminate until the method of execution returns and my program continues.
Popen()
with shell=True
does not block but also does not indicate failure. The subprocess
helper functions do block and are not useful here. Popen.poll()
(suggested in this question) returns None
until the command terminates and does not immediately return non-None
on failure (it must be called repeatedly until the shell terminates).
As an example of the desired behavior
prog1 = shell_run('blocking_prog', stdin=PIPE, stdout=PIPE)
prog2 = shell_run('nonsense_prog', stdin=PIPE, stdout=PIPE)
the first line should assign a Popen
object to prog1
, the second line should raise an OSError
or similar.
Am I correct in thinking that it is impossible to reliably detect Popen()
errors under these conditions?