Peng Yu
2018-02-23 21:11:41 UTC
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?
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
Regards,
Peng