How to clean pipe using bash

I have a script that writes to a named pipe, and another from a pipe. Sometimes, when running the script, I notice that the contents of the channel exist from a previous script run. Is there any way to wash the pipe at the beginning of the script?

+7
bash named-pipes
source share
3 answers

I think dd is your friend:

 dd if=myfifo iflag=nonblock of=/dev/null 

shows strace

 open("myfifo", O_RDONLY|O_NONBLOCK) 

and doesn't even lock on an empty fifo.

+5
source share

You can read from the pipe until it becomes empty. This will cleanse it effectively.

Before you attempt this bold feat, call fcntl(mypipe, F_SETFL, O_NONBLOCK) (I don't know the shell script equivalent) to read when the pool is empty, do not hang your program.

+1
source share

Try the following:

"Opening FD read / write, not just read, when setting up the pipeline prevents locking."

from

Configuring protocol reading from named pipes without blocking in bash

+1
source share

All Articles