Awk doesn't exit despite “exit 0” command












0














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 172.17.0.2/16
...


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?










share|improve this question
























  • Unable to replicate: for i in {10..20}; do echo $i; done | awk '{ exit 0} END { print NR}' works as apparently intended.
    – DopeGhoti
    Dec 17 at 22:43






  • 1




    Similar: How to print only one match of grep when tail -f? or Grep slow to exit after finding match? or make tail -f exit on a broken pipe. Most likely awk exits but your shell is waiting for all processes in the pipeline.
    – Stéphane Chazelas
    Dec 17 at 22:44












  • Your output above indicates docker logs is emitting terminal sequences; are you certain that it isn’t when you pipe it to awk? Maybe pipe it into hexdump just to have a peek.
    – mevets
    Dec 17 at 23:31










  • @mevets I assume you're referring to the syntax highlighting? StackExchange is doing that, the output is plain text.
    – user60561
    Dec 18 at 14:23
















0














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 172.17.0.2/16
...


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?










share|improve this question
























  • Unable to replicate: for i in {10..20}; do echo $i; done | awk '{ exit 0} END { print NR}' works as apparently intended.
    – DopeGhoti
    Dec 17 at 22:43






  • 1




    Similar: How to print only one match of grep when tail -f? or Grep slow to exit after finding match? or make tail -f exit on a broken pipe. Most likely awk exits but your shell is waiting for all processes in the pipeline.
    – Stéphane Chazelas
    Dec 17 at 22:44












  • Your output above indicates docker logs is emitting terminal sequences; are you certain that it isn’t when you pipe it to awk? Maybe pipe it into hexdump just to have a peek.
    – mevets
    Dec 17 at 23:31










  • @mevets I assume you're referring to the syntax highlighting? StackExchange is doing that, the output is plain text.
    – user60561
    Dec 18 at 14:23














0












0








0







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 172.17.0.2/16
...


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?










share|improve this question















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 172.17.0.2/16
...


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?







awk pipe docker coreutils






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 17 at 22:51









Jeff Schaller

38.7k1053125




38.7k1053125










asked Dec 17 at 22:07









user60561

1355




1355












  • Unable to replicate: for i in {10..20}; do echo $i; done | awk '{ exit 0} END { print NR}' works as apparently intended.
    – DopeGhoti
    Dec 17 at 22:43






  • 1




    Similar: How to print only one match of grep when tail -f? or Grep slow to exit after finding match? or make tail -f exit on a broken pipe. Most likely awk exits but your shell is waiting for all processes in the pipeline.
    – Stéphane Chazelas
    Dec 17 at 22:44












  • Your output above indicates docker logs is emitting terminal sequences; are you certain that it isn’t when you pipe it to awk? Maybe pipe it into hexdump just to have a peek.
    – mevets
    Dec 17 at 23:31










  • @mevets I assume you're referring to the syntax highlighting? StackExchange is doing that, the output is plain text.
    – user60561
    Dec 18 at 14:23


















  • Unable to replicate: for i in {10..20}; do echo $i; done | awk '{ exit 0} END { print NR}' works as apparently intended.
    – DopeGhoti
    Dec 17 at 22:43






  • 1




    Similar: How to print only one match of grep when tail -f? or Grep slow to exit after finding match? or make tail -f exit on a broken pipe. Most likely awk exits but your shell is waiting for all processes in the pipeline.
    – Stéphane Chazelas
    Dec 17 at 22:44












  • Your output above indicates docker logs is emitting terminal sequences; are you certain that it isn’t when you pipe it to awk? Maybe pipe it into hexdump just to have a peek.
    – mevets
    Dec 17 at 23:31










  • @mevets I assume you're referring to the syntax highlighting? StackExchange is doing that, the output is plain text.
    – user60561
    Dec 18 at 14:23
















Unable to replicate: for i in {10..20}; do echo $i; done | awk '{ exit 0} END { print NR}' works as apparently intended.
– DopeGhoti
Dec 17 at 22:43




Unable to replicate: for i in {10..20}; do echo $i; done | awk '{ exit 0} END { print NR}' works as apparently intended.
– DopeGhoti
Dec 17 at 22:43




1




1




Similar: How to print only one match of grep when tail -f? or Grep slow to exit after finding match? or make tail -f exit on a broken pipe. Most likely awk exits but your shell is waiting for all processes in the pipeline.
– Stéphane Chazelas
Dec 17 at 22:44






Similar: How to print only one match of grep when tail -f? or Grep slow to exit after finding match? or make tail -f exit on a broken pipe. Most likely awk exits but your shell is waiting for all processes in the pipeline.
– Stéphane Chazelas
Dec 17 at 22:44














Your output above indicates docker logs is emitting terminal sequences; are you certain that it isn’t when you pipe it to awk? Maybe pipe it into hexdump just to have a peek.
– mevets
Dec 17 at 23:31




Your output above indicates docker logs is emitting terminal sequences; are you certain that it isn’t when you pipe it to awk? Maybe pipe it into hexdump just to have a peek.
– mevets
Dec 17 at 23:31












@mevets I assume you're referring to the syntax highlighting? StackExchange is doing that, the output is plain text.
– user60561
Dec 18 at 14:23




@mevets I assume you're referring to the syntax highlighting? StackExchange is doing that, the output is plain text.
– user60561
Dec 18 at 14:23










1 Answer
1






active

oldest

votes


















2














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!






share|improve this answer























  • 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!
    – user60561
    Dec 18 at 14:25













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489576%2fawk-doesnt-exit-despite-exit-0-command%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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!






share|improve this answer























  • 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!
    – user60561
    Dec 18 at 14:25


















2














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!






share|improve this answer























  • 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!
    – user60561
    Dec 18 at 14:25
















2












2








2






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!






share|improve this answer














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!







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 18 at 1:24

























answered Dec 17 at 23:13









wef

913




913












  • 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!
    – user60561
    Dec 18 at 14:25




















  • 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!
    – user60561
    Dec 18 at 14:25


















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!
– user60561
Dec 18 at 14:25






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!
– user60561
Dec 18 at 14:25




















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489576%2fawk-doesnt-exit-despite-exit-0-command%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre