如何使用管道中的生产者进程重新启动消费者进程

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

我正好有这个:

set -eo pipefail;

while true; do
    echo '(re)starting server...'
    (
    while read line; do
    if test "$line" == 'rs'; then
       echo 'restarting...' > /dev/stderr
       exit 0
    fi
    done
    ) |  go run src/main/main.go
done;

但是当生产者中调用 exit 0 时,go 消费者并不会死亡。 有没有办法捕获消费者的信号?

bash shell pipe signals sigpipe
1个回答
0
投票

不要在子 shell 中运行 while-read 循环,因为那是您要退出的 shell。请参阅 https://mywiki.wooledge.org/BashFAQ/024 了解更多信息。

试试这个:

while true; do
    echo '(re)starting server...'
    go run src/main/main.go < <(
        while read line; do
            if test "$line" == 'rs'; then
                 echo 'restarting...' > /dev/stderr
                 exit 0
            fi
        done
    )
done
© www.soinside.com 2019 - 2024. All rights reserved.