These are the steps to reproduce the problem.
writer
:
#!/bin/bash
trap 'echo NoReader!' PIPE
cat > fifo
Open two terminals.
I will denote them by the >T1<
and >T2<
headers below,
and their prompts by $
.
>T1<
$ mkfifo fifo
$ bash writer
ABC
>T2<
$ cat fifo
ABC
^C
>T1<
DEF
$ echo $?
141
From man fifo
,
When a process tries to write to a FIFO that is not opened for read on the other side, the process is sent a SIGPIPE signal.
At the time I enter DEF
, the FIFO has no reader anymore. So
I expected the trap on SIGPIPE to be triggered after entering DEF
and the corresponding NoReader!
message. Instead, the process silently terminates. The error code is 141, which
indicates that it was terminated indeed by SIGPIPE.
On the other hand, executing this newWriter
#!/bin/bash
trap 'echo NoReader!' PIPE
var=$(head -c 100000 /dev/urandom)
echo "$var" > fifo
in terminal 1 and head -c 1 fifo
in terminal 2 does trigger the trap! However, if I just extract 1000 bytes from urandom instead of 100000, the trap is not triggered.
What am I missing? Why wasn't the trap triggered in the first writer
example, but was in the newWriter
with 100000 bytes (and not with 1000 bytes)?
newWriter
's trap was triggered because what was used to write to the FIFO wasecho
, a shell builtin, so the shell caught the signal. – Quasímodo Jul 28 '20 at 15:56