help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Where the complete behavior of & is documented?


From: John McKown
Subject: Re: [Help-bash] Where the complete behavior of & is documented?
Date: Fri, 23 Feb 2018 15:21:15 -0600

I'm not an expert, but I think what you are seeing is because of the
"process group" concept in UNIX.

http://www.informit.com/articles/article.aspx?p=397655&seqNum=6


On Fri, Feb 23, 2018 at 3:11 PM, Peng Yu <address@hidden> wrote:

> Hi,
>
> I have the following testing script.
>
> $ cat main1.sh
> #!/usr/bin/env bash
> # vim: set noexpandtab tabstop=2:
>
> set -v
> echo "$BASHPID"
> awk -e 'BEGIN { for(i=0;;i++) { system("sleep 5"); print i } }' &
> pid=$!
> echo "$pid"
> wait
>
> When I run it, I see
> $ ./main1.sh
> echo "$BASHPID"
> 49839
> awk -e 'BEGIN { for(i=0;;i++) { system("sleep 5"); print i } }' &
> pid=$!
> echo "$pid"
> 49840
> wait
> 0
> 1
> 2
> ...
>
> When I check the processes, I see
>
> $ ps -f | grep 49839 |head -n 2
>   504 49839 48607   0  2:50PM ttys021    0:00.04 bash ./main1.sh
>   504 49840 49839   0  2:50PM ttys021    0:00.02 awk -e BEGIN {
> for(i=0;;i++) { system("sleep 5"); print i } }
>
> Now, once I type ^C at the terminal running ./main1.sh, I don't find
> the process 49839 anymore.
>
> Running `jobs` in this terminal, show no jobs running in the
> background despite 49840 is still running. Note that the pid of the
> parent of 49840 becomes 1.
>
> $ ps -f | grep 49840 | head -n 2
>   504 49840     1   0  2:50PM ttys021    0:00.03 awk -e BEGIN {
> for(i=0;;i++) { system("sleep 5"); print i } }
>   504 50005 49840   0  2:52PM ttys021    0:00.01 sleep 5
>
> Then, when I type `exit` in the terminal used to run ./main1.sh, the
> awk process 49840 will be terminated as well.
>
> This means that somehow, bash understand the difference between the
> processing running main1.sh and the terminal process from which
> main1.sh is called.
>
> But how does bash know to kill awk upon exit, since the parent pid of
> the awk process is 1. It sounds like bash shouldn't know it. So this
> is confusing to me.
>
> I checked the section of "Lists" and "JOB CONTROL". I don't where this
> is clearly document.
>
> Does anybody know where this is documented in bash manpage?
>
> --
> Regards,
> Peng
>
>


-- 
I have a theory that it's impossible to prove anything, but I can't prove
it.

Maranatha! <><
John McKown


reply via email to

[Prev in Thread] Current Thread [Next in Thread]