Trap SIGPIPE when trying to write without reader
        Posted  
        
            by 
                Matt
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Matt
        
        
        
        Published on 2014-06-13T09:20:18Z
        Indexed on 
            2014/06/13
            9:25 UTC
        
        
        Read the original article
        Hit count: 354
        
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.
© Stack Overflow or respective owner