1

I have two scripts

foox.sh

#!/bin/bash

echo "foox"
./fooy.sh &
pid=${!}
echo "waiting on ${pid}"
timeout 4 wait ${pid}
echo "waited on ${pid} with result ${!}"

and fooy.sh

#!/bin/bash

echo "fooy.sh"
sleep 99999999

The objective is for foox.sh to wait on fooy.sh for 4 seconds then time out with a non zero exit code on the timeout command.

However, I get that the PID of the fooy.sh process in a not a child process of foox.sh which makes no sense to me, and thus the question.

Here is standard output:

foox
waiting on 48145
fooy.sh
/usr/bin/wait: line 4: wait: pid 48145 is not a child of this shell
waited on 48145 with result 48145

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
the_prole
  • 8,275
  • 16
  • 78
  • 163
  • Have a look here... https://unix.stackexchange.com/a/427133 – Mark Setchell Jan 27 '20 at 19:10
  • 1
    The `wait` must be executed by the process that created the background PID. When you use `timeout 4 wait ${pid}`, you're running a new process (`timeout`) and it has no child process with PID equal to `${pid}`. Any workaround is going to be complex. Note that a process can only wait for its own children to die; it cannot wait for grandchildren to die, nor for siblings, nor for parents. – Jonathan Leffler Jan 27 '20 at 19:11
  • Circular cross-site links — the [Unix & Linux](https://unix.stackexchange.com/q/427115/15168) question links back to SO and [Wait for a process to finish](https://stackoverflow.com/q/1058047/15168). – Jonathan Leffler Jan 27 '20 at 19:14

0 Answers0