help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Why is backgrounded work not considered a 'job' if launc


From: Hales
Subject: Re: [Help-bash] Why is backgrounded work not considered a 'job' if launched inside of something being piped to?
Date: Fri, 28 Dec 2018 08:30:58 +1100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.3

Thankyou Greg Wooledge, this makes sense.

Confirming this explanation: it appears that job management works inside the loops themselves (before the loop terminates).

Regards, Hales


On 28/12/18 5:07 am, Greg Wooledge wrote:
On Thu, Dec 27, 2018 at 10:30:45PM +1100, Hales wrote:
echo -e 'one\ntwo\nthree' | while read line
do
     example_worker $line &
done
Because the pipeline creates two subshells.  Your background jobs are
executed from within one of these subshells.  When that subshell exits,
there goes the parent of the background jobs.  With no parent, they are
orphaned, and adopted by init, and your main shell process (their
grandparent) is NOT able to wait for them.

You must rearrange your code so that the background jobs are NOT run
from within a subshell.

For example, this is a common idiom:

while read -r line; do ...
   foo &
done < <(some process that is not echo)





reply via email to

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