1

I am trying to implement a named-pipe communication solution between two processes in Bash.

The first process writes something to the named pipe:

send(){
    echo 'something' > $NAMEDPIPE
}

And the second script is supposed to read the named pipe like this:

while true;do
  if read line < $NAMEDPIPE;do
      someCommands
  fi
done

Note 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 tries to write to the named-pipe it will stay blocked until a reader connects to 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 sent 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 stays blocked, and "SIGPIPE received" does not get printed.

Am I mistaking on the signal mechanism or is there any better solution to my problem?

4

1 に答える 1