Linux – Execute command in-between a bash pipe row …

bashcommand linelinuxpipe

I want to run a series of piped commands which do a lot of audioprocessing. It looks as follows in pseudocode:

command1 | command2 | command3

Of command2 and command3, both take the output of the previous command as input (regular stdin/stdout behaviour).

Now I'd like to execute another command1.1 right after command1, which is in no way connected to the running pipe between command1 and command2, except for it must not be run before command1 has completely finished.

command1 [; after that run command1.1, even if command2 and command3 are still busy] command2 command3

However I have no idea how to do this in bash. I've tried tee, but that makes the command to be run as soon as the pipe is constructed. Any ideas?

Thanks!

Best Answer

You can redirect it to a subshell.

command1 & > >(command2 | command3) &
wait $!      # Wait end of process $! (actually the pid of command1)
command_1.1     

Another way is to create a named pipe fifo.
A script should be similar to

MYFIFO=/tmp/myfifo.$$
rm -f $MYFIFO
mkfifo $MYFIFO
command1 > $MYFIFO &
MYPROGRAM_PID=$!
cat $MYFIFO  | command2 | command3 &
wait $MYPROGRAM_PID   # Wait end of process $MYPROGRAM_PID
command_1.1     
Related Question