我有这个确切的代码:
#!/bin/bash
gtimeout(){
if type -f gtimeout &> /dev/null; then
command gtimeout "$@"
else
timeout "$@"
fi
}
export -f gtimeout;
on_first_match(){
local pattern="$1" # The pattern to search for
while IFS= read -r line; do
if [[ "$line" == *"$pattern"* ]]; then
exit 0
fi
done
}
export -f on_first_match
write_to_server(){
echo -e "$?" | nc 0.0.0.0 3333
}
export -f write_to_server
(
set +e;
sleep 2;
(
gtimeout 15 docker logs -f rabbitmq-node1 | on_first_match 'Starting listener on';
write_to_server
) &
(
gtimeout 15 docker logs -f mongodb-node1 | on_first_match 'waiting for connections';
write_to_server
) &
(
gtimeout 15 docker logs -f elasticsearch-node1 | on_first_match 'started';
write_to_server
) &
(
gtimeout 15 docker logs -f rabbitmq-node2 | on_first_match 'Starting listener on';
write_to_server
) &
(
gtimeout 15 docker logs -f mongodb-node2 | on_first_match 'waiting for connections';
write_to_server
) &
(
gtimeout 15 docker logs -f elasticsearch-node2 | on_first_match 'started';
write_to_server
) &
(
gtimeout 15 docker logs -f rabbitmq-node3 | on_first_match 'Starting listener on';
write_to_server
) &
(
gtimeout 15 docker logs -f mongodb-node3 | on_first_match 'waiting for connections';
write_to_server
) &
(
gtimeout 15 docker logs -f elasticsearch-node3 | on_first_match 'started';
write_to_server
) &
) &
gtimeout 30 nc -k -l 3333 | while IFS= read -r line; do
echo "Received: $line";
done
我得到的输出是这样的:
Error response from daemon: No such container: mongodb-node1
Error response from daemon: No such container: rabbitmq-node2
Error response from daemon: No such container: rabbitmq-node1
Error response from daemon: No such container: elasticsearch-node2
Error response from daemon: No such container: elasticsearch-node1
Error response from daemon: No such container: mongodb-node2
Error response from daemon: No such container: mongodb-node3
Error response from daemon: No such container: rabbitmq-node3
Error response from daemon: No such container: elasticsearch-node3
Received: 0
Received: 0
Received: 0
Received: 0
Received: 0
(仅收到 5 条线,有时 4 条,有时 6 条,但从来没有像预期的那样收到 9 条)
但我期望这样:
Error response from daemon: No such container: mongodb-node1
Error response from daemon: No such container: rabbitmq-node2
Error response from daemon: No such container: rabbitmq-node1
Error response from daemon: No such container: elasticsearch-node2
Error response from daemon: No such container: elasticsearch-node1
Error response from daemon: No such container: mongodb-node2
Error response from daemon: No such container: mongodb-node3
Error response from daemon: No such container: rabbitmq-node3
Error response from daemon: No such container: elasticsearch-node3
Received: 0
Received: 0
Received: 0
Received: 0
Received: 0
Received: 0
Received: 0
Received: 0
Received: 0
有 9 条接收线。
有人知道这是怎么回事吗? (当我将代码切换为 mkfifo 而不是套接字时,会发生相同的行为)。