I have the following command:

sudo docker logs -f <id> | awk '/Listening on / {print $3; exit 0;}'

From what I understand, awk should find the first line that matches the pattern, print the correct column, and exit. However, this does not always happen: most the time it simply prints the correct column and hangs, although occasionally it exits correctly.

I've tried

$ sudo stdbuf -i0 -o0 -e0 docker logs ...  # hangs
$ sudo docker logs ... | cat | awk ... # hangs

In trying to produce a minimal example, I also tried making a pipe using mkfifo, and using cat foo | awk ... but that doesn't hang.

The output from docker logs -f <id> is

Listening on

and then it remains open, but doesn't write any thing else.

I don't understand what's going on--does anyone have any ideas?


1 Answer 1


From man bash:

The shell waits for all commands in the pipeline to terminate before returning a value.

I don't have docker installed but maybe try this:

awk '/Listening on / {print $3; exit 0;}' <( sudo docker logs -f <id> )

Or you could try this:

sudo docker logs -f <id> | (awk '/Listening on / {print $3; exit 0;}'; pkill -P $$ )

So perhaps an explanation of what is going on would help. I'm assuming that the 'docker logs' command does not normally exit (the -f == follow?).

The original problem arises because the pipeline is not exiting on awk's termination - this is because the pipeline is still waiting on the docker program (see my extract above from the bash man page).

The pkill simply kills anything that has parent id $$ - ie at that moment, that is just docker.

Obviously, it would not be a good idea to chuck that command into background with '&' and then run other things - they would also get killed!

  • Yup, this is exactly it! This behavior is surprising, although not unexpected--the only way it could work otherwise is if bash sent SIGINT or SIGQUIT to the other programs in the pipe, and I've never seen that happen. Thank you!
    – flaviut
    Dec 18, 2018 at 14:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .