I want to use a pipe with mkfifo
to capture all output from several processes running concurrently, roughly like the following
PIPENAME="/tmp/foo"
echo "Make ${PIPENAME:?}" && mkfifo "${PIPENAME:?}"
echo "hello 1" >"${PIPENAME:?}" &
echo "hello 2" >"${PIPENAME:?}" &
wait < <(jobs -p)
cat "${PIPENAME:?}" | sort | uniq
echo "Removing FIFO ${PIPENAME:?}" && rm "${PIPENAME:?}"
However, the above hangs and does not work, that is, none of the echo "hello..." >"${PIPENAME}"
processes terminate so the script never gets to the wait line.
I guess a process writing to the pipe cannot terminate until its output was consumed from the other end? If yes, what's the canonical way to achieve my goal here? Again, I want to be able to start a few (less than 20) possibly long-running commands in parallel and have their outputs combined without loosing any lines.