我正好有这个:
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 消费者并不会死亡。 有没有办法捕获消费者的信号?
不要在子 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