收听已删除的fifo /命名管道的EOF

问题描述 投票:1回答:1

我创建了这个fifo /命名管道“

my_named_pipe="$HOME/foobar"
mkfifo "$my_named_pipe"

while read line; do on_fifo_msg "$line"; done < ${my_named_pipe} &

在稍后的某个时刻,我从文件系统中删除了这个fifo - 我假设读取循环因为EOF或其他原因而结束 - 但我怎么能听到那个事件呢?如何在读取循环结束时监听?

bash shell named-pipes eof fifo
1个回答
1
投票

我认为while read循环没有办法检测自动删除的FIFO。

您可以使用另一个循环来定期检查FIFO是否仍然存在,并终止读取循环:

while read line; do on_fifo_msg "$line"; done < ${my_named_pipe} &
read_pid=$!
while kill -0 $read_pid; do
    if ! [[ -e "$my_named_pipe" ]];
    then kill $read_pid
    fi
    sleep 1
done &

kill -0 $read_pid测试读循环过程是否仍然存在。

© www.soinside.com 2019 - 2024. All rights reserved.