Background process of subshell strange behaviour

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I hope the title is correct and not misleading, I've tried to be specific about something which I don't know very much about. To be more general, I wonder why some similar bash commands behave the way they do.
I have a bash script foo:
#!/usr/bin/env bash
while true
do
echo "reading"
read data
echo $data
echo "stderr msg" >&2
sleep 1
done
It's an infinite loop which reads one line at a time from stdin and outputs that same line. I also have a bash script bar:
#!/usr/bin/env bash
./foo &
The following commands were run in bash (v. 4.4.19) in a terminal on Ubuntu 18.04 (let's assume the scripts reside in the working directory):
1) ./foo &
Stops when it tries to read from stdin, in accordance with this answer.
Killed when controlling terminal is killed, as expected.
2) (./foo &)
Appears to be automagically continuously fed with new input from stdin. Shouldn't it too stop when attempting to read from stdin? Whatever it's getting it doesn't show when echoed, thus I'm guessing it's EOF characters.
Continues running even after the controlling terminal is killed. Looking at the output of
ps, the controlling terminal changes from pts/x to ?. How did this happen without the use of eitherdisownornohup? (However, after killing the originally controlling terminal, it yields a "write error: Input/output error" on every write, which I suppose is because its stdout was tied to the now closed terminal. Correct?)
3) bash -c "./foo &"
Appears to behave exactly like 2).
4) ./bar
Appears to behave exactly like 2) and 3).
5) Adding bash -c "~/foo" (no &) as a startup application
Behaves similarly to 2), 3) and 4), with the differences:
- Its controlling terminal is the desktop ttyx.
- You can't (can you?) kill its controlling terminal.
None of those differences seem odd to me, just the fact that it's continuously fed with new input.
My guess is that 2) - 5) all use a subshell of sorts whose stdin is redirected to something like /dev/null, though I'm very unsure and seek a more exact answer.
bash background-process subshell
add a comment |Â
up vote
1
down vote
favorite
I hope the title is correct and not misleading, I've tried to be specific about something which I don't know very much about. To be more general, I wonder why some similar bash commands behave the way they do.
I have a bash script foo:
#!/usr/bin/env bash
while true
do
echo "reading"
read data
echo $data
echo "stderr msg" >&2
sleep 1
done
It's an infinite loop which reads one line at a time from stdin and outputs that same line. I also have a bash script bar:
#!/usr/bin/env bash
./foo &
The following commands were run in bash (v. 4.4.19) in a terminal on Ubuntu 18.04 (let's assume the scripts reside in the working directory):
1) ./foo &
Stops when it tries to read from stdin, in accordance with this answer.
Killed when controlling terminal is killed, as expected.
2) (./foo &)
Appears to be automagically continuously fed with new input from stdin. Shouldn't it too stop when attempting to read from stdin? Whatever it's getting it doesn't show when echoed, thus I'm guessing it's EOF characters.
Continues running even after the controlling terminal is killed. Looking at the output of
ps, the controlling terminal changes from pts/x to ?. How did this happen without the use of eitherdisownornohup? (However, after killing the originally controlling terminal, it yields a "write error: Input/output error" on every write, which I suppose is because its stdout was tied to the now closed terminal. Correct?)
3) bash -c "./foo &"
Appears to behave exactly like 2).
4) ./bar
Appears to behave exactly like 2) and 3).
5) Adding bash -c "~/foo" (no &) as a startup application
Behaves similarly to 2), 3) and 4), with the differences:
- Its controlling terminal is the desktop ttyx.
- You can't (can you?) kill its controlling terminal.
None of those differences seem odd to me, just the fact that it's continuously fed with new input.
My guess is that 2) - 5) all use a subshell of sorts whose stdin is redirected to something like /dev/null, though I'm very unsure and seek a more exact answer.
bash background-process subshell
Try addingecho $?afterread datato see if the read was successful.
â Barmar
Aug 15 at 18:24
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I hope the title is correct and not misleading, I've tried to be specific about something which I don't know very much about. To be more general, I wonder why some similar bash commands behave the way they do.
I have a bash script foo:
#!/usr/bin/env bash
while true
do
echo "reading"
read data
echo $data
echo "stderr msg" >&2
sleep 1
done
It's an infinite loop which reads one line at a time from stdin and outputs that same line. I also have a bash script bar:
#!/usr/bin/env bash
./foo &
The following commands were run in bash (v. 4.4.19) in a terminal on Ubuntu 18.04 (let's assume the scripts reside in the working directory):
1) ./foo &
Stops when it tries to read from stdin, in accordance with this answer.
Killed when controlling terminal is killed, as expected.
2) (./foo &)
Appears to be automagically continuously fed with new input from stdin. Shouldn't it too stop when attempting to read from stdin? Whatever it's getting it doesn't show when echoed, thus I'm guessing it's EOF characters.
Continues running even after the controlling terminal is killed. Looking at the output of
ps, the controlling terminal changes from pts/x to ?. How did this happen without the use of eitherdisownornohup? (However, after killing the originally controlling terminal, it yields a "write error: Input/output error" on every write, which I suppose is because its stdout was tied to the now closed terminal. Correct?)
3) bash -c "./foo &"
Appears to behave exactly like 2).
4) ./bar
Appears to behave exactly like 2) and 3).
5) Adding bash -c "~/foo" (no &) as a startup application
Behaves similarly to 2), 3) and 4), with the differences:
- Its controlling terminal is the desktop ttyx.
- You can't (can you?) kill its controlling terminal.
None of those differences seem odd to me, just the fact that it's continuously fed with new input.
My guess is that 2) - 5) all use a subshell of sorts whose stdin is redirected to something like /dev/null, though I'm very unsure and seek a more exact answer.
bash background-process subshell
I hope the title is correct and not misleading, I've tried to be specific about something which I don't know very much about. To be more general, I wonder why some similar bash commands behave the way they do.
I have a bash script foo:
#!/usr/bin/env bash
while true
do
echo "reading"
read data
echo $data
echo "stderr msg" >&2
sleep 1
done
It's an infinite loop which reads one line at a time from stdin and outputs that same line. I also have a bash script bar:
#!/usr/bin/env bash
./foo &
The following commands were run in bash (v. 4.4.19) in a terminal on Ubuntu 18.04 (let's assume the scripts reside in the working directory):
1) ./foo &
Stops when it tries to read from stdin, in accordance with this answer.
Killed when controlling terminal is killed, as expected.
2) (./foo &)
Appears to be automagically continuously fed with new input from stdin. Shouldn't it too stop when attempting to read from stdin? Whatever it's getting it doesn't show when echoed, thus I'm guessing it's EOF characters.
Continues running even after the controlling terminal is killed. Looking at the output of
ps, the controlling terminal changes from pts/x to ?. How did this happen without the use of eitherdisownornohup? (However, after killing the originally controlling terminal, it yields a "write error: Input/output error" on every write, which I suppose is because its stdout was tied to the now closed terminal. Correct?)
3) bash -c "./foo &"
Appears to behave exactly like 2).
4) ./bar
Appears to behave exactly like 2) and 3).
5) Adding bash -c "~/foo" (no &) as a startup application
Behaves similarly to 2), 3) and 4), with the differences:
- Its controlling terminal is the desktop ttyx.
- You can't (can you?) kill its controlling terminal.
None of those differences seem odd to me, just the fact that it's continuously fed with new input.
My guess is that 2) - 5) all use a subshell of sorts whose stdin is redirected to something like /dev/null, though I'm very unsure and seek a more exact answer.
bash background-process subshell
bash background-process subshell
edited Aug 8 at 23:16
Jeff Schaller
32.4k849110
32.4k849110
asked Aug 8 at 22:27
gblomqvist
61
61
Try addingecho $?afterread datato see if the read was successful.
â Barmar
Aug 15 at 18:24
add a comment |Â
Try addingecho $?afterread datato see if the read was successful.
â Barmar
Aug 15 at 18:24
Try adding
echo $? after read data to see if the read was successful.â Barmar
Aug 15 at 18:24
Try adding
echo $? after read data to see if the read was successful.â Barmar
Aug 15 at 18:24
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
Your sub shells are non-interactive; only interactive shells are automatically killed.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Your sub shells are non-interactive; only interactive shells are automatically killed.
add a comment |Â
up vote
0
down vote
Your sub shells are non-interactive; only interactive shells are automatically killed.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Your sub shells are non-interactive; only interactive shells are automatically killed.
Your sub shells are non-interactive; only interactive shells are automatically killed.
edited Aug 8 at 23:36
Jeff Schaller
32.4k849110
32.4k849110
answered Aug 8 at 22:55
user1133275
2,277412
2,277412
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461386%2fbackground-process-of-subshell-strange-behaviour%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Try adding
echo $?afterread datato see if the read was successful.â Barmar
Aug 15 at 18:24