help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Waiting for a sub-process to finish


From: João Eiras
Subject: Re: [Help-bash] Waiting for a sub-process to finish
Date: Thu, 1 Jun 2017 22:47:14 +0200

On 1 June 2017 at 22:35, Chet Ramey <address@hidden> wrote:
>
> On 6/1/17 4:27 PM, Greg Wooledge wrote:
>
> > Other than that, bash 4.4 added this new feature:
> >
> > u.  Bash now allows waiting for the most recent process substitution, since 
> > it
> >     appears as $!.
> >
> > I believe you have to explicitly pass the PID to wait for those, rather
> > than just calling wait with 0 arguments, but it's not something I've
> > played with much.
>
> It's always appeared as $!, and you can wait for it the same as any other
> process. The changelog entry refers to wait without arguments, which is
> supposed to wait for all background processes.
>

I'll try bash 4.4 when I get the change, but not an option to upgrade
now, since I have to run the script in many machines. Portability is
not an issue as the machines all have the same configuration.

I just tried in bash 4.3 "wait $!" and I got an error message

  my_command_1 <(my_command_2) > >(my_command_3)
  wait $!

wait: pid XXXXX is not a child of this shell

So, I think the feature does not work at all in bash 4.3. But I'm
happy to know it has been added/fixed for future versions.

Using a namedpipe is something I want to avoid. This section of the
script is called hundreds of time in parallel, so the less poking the
file system with temporary files, the better.

I came up with something that I had been using elsewhere, by
implementing my own "wait". Not pretty but it works

Thank you very much for your time !
______
function wait_for_subprocesses {
  function get_subprocesses {
    # @param[1] parent pid
    # @param[2] blacklisted pid
    for p in $(pgrep -P $1); do
      [[ "$p" = "$2" ]] && continue
      echo $p
      get_subprocesses "$p" "$2"
    done
  }
  local thispid=$BASHPID
  while [[ 1 ]]; do
    local sub=( $(get_subprocesses $thispid $BASHPID) )
    [[ address@hidden = 0 ]] && break
    sleep 0.005
  done
}



reply via email to

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