4

我有这个脚本名为daemon.fish

function --on-signal SIGTERM on-signal-sigterm
  echo "SIGTERM captured"
end

set fifo /tmp/asd.fifo

echo "fifo reader [" %self "]"

while test ! -p $fifo
  echo "$fifo is not a fifo" >&2
  sleep 1
end

while true
  if read --local line < $fifo
    echo "line: $line" &
  end
end

如果我启动脚本,则在执行第二个循环时我无法捕获信号(fe SIGTERM):

$ fish daemon.fish
> fifo reader [ 15853 ]
> /tmp/asd.fifo is not a fifo
> ...
$ kill -SIGTERM 15853 [in another shell]
> SIGTERM captured
> /tmp/asd.fifo is not a fifo
> ...
$ mkfifo /tmp/asd.fifo
$ kill -SIGTERM 15853 [in another shell]
> [nothing]

bash 版本有效,所以我猜这个问题是特定于鱼的:

trap "echo SIGTERM captured" SIGTERM

fifo=/tmp/asd.fifo

echo "fifo reader [" $$ "]"

while [[ ! -p $fifo ]]; do
  echo "$fifo is not a fifo" >&2
  sleep 1
done

while true; do
  if read line < $fifo; then
    echo "line: $line" &
  fi
done

测试:

$ rm -f /tmp/asd.txt; and fish daemon.bash
> fifo reader [ 15853 ]
> /tmp/asd.fifo is not a fifo
> ...
$ kill -SIGTERM 15853 [in another shell]
> SIGTERM captured
> /tmp/asd.fifo is not a fifo
> ...
$ mkfifo /tmp/asd.fifo
$ kill -SIGTERM 15853 [in another shell]
> daemon.bash: line 13: /tmp/asd.fifo: Interrupted system call
> SIGTERM captured
4

0 回答 0