鱼壳:从命名管道读取时捕获信号

时间:2014-03-17 17:20:39

标签: shell signals named-pipes fish

我有一个名为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

如果我启动脚本,我就无法在执行第二个循环时捕获信号(f.e. 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

0 个答案:

没有答案