Trap SIGPIPE when trying to write without reader
- by Matt
I am trying to implement a named-pipe communication solution in BASH between two processes.
The first process runs a script which echo something in a named-pipe:
send(){
echo 'something' > $NAMEDPIPE
}
And the second script is supposed to read the named-pipe via another script which contains:
while true;do
if read line < $NAMEDPIPE;do
someCommands
fi
done
Not that the named pipe has been previously created using the traditional command
mkfifo $NAMEDPIPE
My problem is that the reader script is not always running so that if the writer script try to write in the named-pipe it stay blocked until a reader connect the pipe.
I want to avoid this behavior, and a solution would be to trap a SIGPIPE signal. Indeed, according to man 7 signal is supposed to be send when trying to write in a pipe with no reader. So I changed my red function by:
read(){
trap 'echo "SIGPIPE received"' SIGPIPE
echo 'something' > $NAMEDPIPE
}
But when I run the reader script, the script stay blocked, and not "SIGPIPE received" appears...
Am I mistaking on the signal mechanism or is there any better solution to my problem ?
Thank you for your help.