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:
$ mkfifo /tmp/foobar.pipe
$ some_program --command-fd=5 5<>/tmp/foobar.pipe
prevents the undesired blocking behavior, as 5<>/tmp/foobar.pipe
opens in RW mode (as opposed to opening in read-only mode as with 5</tmp/foobar.pipe
) although O_NONBLOCK
is still set. Thanks to waldner on irc://irc.freenode.org/#bash for this pointer.
The only way I know getting this kind of result is a hack:
mkfifo /tmp/foobar.in
mkfifo /tmp/foobar.out
( cat </tmp/foobar.in ) >/tmp/foobar.out &
some_program --command-fd=5 5</tmp/foobar.out
perhaps this helps :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With