Background process of subshell strange behaviour

The name of the pictureThe name of the pictureThe name of the pictureClash 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 either disown or nohup? (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.










share|improve this question























  • Try adding echo $? after read data to see if the read was successful.
    – Barmar
    Aug 15 at 18:24














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 either disown or nohup? (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.










share|improve this question























  • Try adding echo $? after read data to see if the read was successful.
    – Barmar
    Aug 15 at 18:24












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 either disown or nohup? (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.










share|improve this question















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 either disown or nohup? (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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 8 at 23:16









Jeff Schaller

32.4k849110




32.4k849110










asked Aug 8 at 22:27









gblomqvist

61




61











  • 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















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










1 Answer
1






active

oldest

votes

















up vote
0
down vote













Your sub shells are non-interactive; only interactive shells are automatically killed.






share|improve this answer






















    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f461386%2fbackground-process-of-subshell-strange-behaviour%23new-answer', 'question_page');

    );

    Post as a guest






























    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.






    share|improve this answer


























      up vote
      0
      down vote













      Your sub shells are non-interactive; only interactive shells are automatically killed.






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        Your sub shells are non-interactive; only interactive shells are automatically killed.






        share|improve this answer














        Your sub shells are non-interactive; only interactive shells are automatically killed.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 8 at 23:36









        Jeff Schaller

        32.4k849110




        32.4k849110










        answered Aug 8 at 22:55









        user1133275

        2,277412




        2,277412



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            Peggy Mitchell

            The Forum (Inglewood, California)

            Palaiologos