bug-bash
[Top][All Lists]
Advanced

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

Re: jobs -p falsely reports the last background pid


From: Pierre Gaston
Subject: Re: jobs -p falsely reports the last background pid
Date: Wed, 9 Apr 2014 15:23:45 +0300

On Wed, Apr 9, 2014 at 3:16 PM, Håkon Bugge <hakon.bugge@gmail.com> wrote:

>
> On 9. apr. 2014, at 14.04, Greg Wooledge wrote:
>
> > On Wed, Apr 09, 2014 at 12:43:40PM +0200, Håkon Bugge wrote:
> >> This script never terminates:
> >> --------------------------------------------------
> >> #!/bin/bash
> >>
> >> for P in `seq 5`; do
> >>    sleep 1&
> >> done
> >>
> >> while true; do
> >>    usleep 20000
> >>    set foo `jobs -p`
> >>    LEFT=$#
> >>    LEFT=$[LEFT-1]
> >>    echo $LEFT jobs left
> >>    if [ x$LEFT = x0 ]; then
> >>      break
> >>    fi
> >> done
> >
> >>   Pasting the same commands in an interactive shell, its works.
> >
> > Interactive shells enable job control (monitor mode), whereas
> > noninteractive shells (scripts) do not.  If you want to use job control
> > commands (like "jobs") within a script, you must enable monitor mode
> > (set -m, or set -o monitor, or #!/bin/bash -m).
>
> That is not the issue. Try it out.
>
>
> Håkon
>

I think greg hints that what "jobs" should do without set -m is not defined
...I wonder if it's really the case

However enabling set -m doesn't help

I agree that your script triggers a strange behavior:

Funnily :
bash -c 'sleep 1& sleep 1& sleep 1& sleep 3; jobs -p' # doesn't print
anything

while if you add just "jobs -p" in your loop without capturing it always
print all the pids


reply via email to

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