I’m looking to call a subprocess with a file descriptor opened to a given pipe such that the open() call does not hang waiting for the other side of the pipe to receive a connection.
To demonstrate:
$ mkfifo /tmp/foobar.pipe $ some_program --command-fd=5 5</tmp/foobar.pipe
In this case, some_program is not run until some process has /tmp/foobar.pipe open for write; however, some_program has useful effects even when it isn’t receiving commands, so desired behavior is for some_program to be immediately executed.
Mechanisms to do this by exec’ing through an alternate scripting language (python, perl, etc) or a C wrapper which open /tmp/foobar.pipe with the O_NONBLOCK flag are obvious; I’m looking for a pure-bash solution, should one be possible.
Opening the FD read/write rather than read-only when setting up the pipeline prevents blocking.
To be a bit more specific:
prevents the undesired blocking behavior, as
5<>/tmp/foobar.pipeopens in RW mode (as opposed to opening in read-only mode as with5</tmp/foobar.pipe) althoughO_NONBLOCKis still set. Thanks to waldner on irc://irc.freenode.org/#bash for this pointer.