4

I know if a subprocess does not get reaped properly, it will become a zombie and you can see it by ps command.

Also the "wait [pid]" command will wait for subshell running on the background until it finishes and reap it.

I have a script like this:

#!/bin/bash
sleep 5 &

tail -f /dev/null

My question is, I don't use wait after sleep 5 & and the parent shell will never terminate because of tail, then why the sleep 5 & will not become a zombie? I see it disappear after finishing in ps, not sure who reaps it?

chengdol
  • 183
  • 4
    Is this answered by https://unix.stackexchange.com/questions/538958/bash-when-is-a-pid-exactly-freed-up ? [Look for Unfortunately]. – Paul_Pedant Apr 11 '21 at 08:33

2 Answers2

3

This is one of the pitfalls with having a shell mimic the OS API. It does create confusion. In this case you are confusing wait() (linux API) with wait (bash function).

Simply enough, Bash takes care of reaping processes for you, you don't generally need to think about this. The wait bash command has no effect on reaping processes.

When your child job terminates, bash will be informed via a SIGCHLD signal. This will when that happens bash will stop whatever it was doing for a moment and reap the stopped child. It then goes back to whatever it was doing.

-1

Your use of "&" requested the parent process to put "sleep 5" in the background, so it is the parent process that will harvest the sleep 5.

waltinator
  • 4,865