3

Bash 5.0 includes a new -f option for wait:[1]

j. The `wait' builtin now has a `-f' option, which signfies to wait until the
   specified job or process terminates, instead of waiting until it changes
   state.

What does wait -f $pid do as opposed to the default wait $pid? Under what conditions is the -f option needed?

Whymarrh
  • 175
  • 1
  • 2
  • 10

1 Answers1

6

The change description is accurate, but somewhat obscure since wait is generally thought of as waiting for a process to finish.

Try this:

sleep 60&
wait %1

then in another terminal,

kill -STOP ${pid}

replacing ${pid} with sleep’s pid (as output when it was put in the background). wait will exit, because the job’s state changed.

With -f, wait will wait for the job or process to really terminate; used above, it wouldn’t exit with kill -STOP, and would wait for the process to be resumed (kill -CONT) and finish running.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • 1
    At least with bash 4.3, this wait-for-any-state-change behavior happens only if the shell is interactive. In a script, fortunately, bash behaves as other shells: wait for the process to terminate. – Gilles 'SO- stop being evil' Jan 08 '19 at 16:36
  • @Gilles is the behaviour in an interactive shell vs. a non-interactive one documented anywhere? `help wait` doesn't mention that difference. – Whymarrh Jan 08 '19 at 17:22
  • @Whymarrh Not in the documentation of 4.3. I haven't checked if it's been fixed in 5.0. – Gilles 'SO- stop being evil' Jan 08 '19 at 17:56
  • @Whymarrh The online manual says "when job control is enabled", which by default is the case for an interactive shell. http://www.gnu.org/software/bash/manual/html_node/Job-Control-Builtins.html – Roland Weber Jun 12 '19 at 10:47