I'm wrapping a terraform binary in a script, as a part of an enterprise solution. Therefore I need to take care of:
- file log capture (separately STDOUT, STDERR for some post-processing analytics)
- live log capture (this runs within the Jenkins job, again separate for STDOUT and STDERR)
- PID capture (as the process needs to run in the background, and in the next step I'll add traps for SIGTERM handling)
Currently, the core construct of the script looks like this:
#!/bin/bash
...
...
terraform "$@" > >(tee "${STDOUT_LOG}") 2> >(tee "${STDERR_LOG}" >&2) & TF_PID="$!"
wait "$TF_PID"
EXIT_CODE="$?"
...
wait
exit "$EXIT_CODE"
This script is called several hundred times in one container. We've noticed it leaves zombie processes, the shells within which tee commands are executed.
Adding a general wait before exiting the script doesn't help, the shell won't wait for these child processes to be reaped. I couldn't read much about the internals of process substitution, would you have a hint what might be going on here?
EDIT:
> ps aux --forest
11295 ? S 0:00 | \_ /bin/bash /home/jenkins/workspace/build-1629@tmp/terraform.sh init
11376 ? Sl 0:01 | | \_ terraform init
11377 ? S 0:00 | | \_ /bin/bash /home/jenkins/workspace/build-1629@tmp/terraform.sh init
11379 ? S 0:00 | | | \_ /usr/bin/coreutils --coreutils-prog-shebang=tee /usr/bin/tee -a /home/jenkins/workspace/build-1629/src/vnet-01/stdout.log
11378 ? S 0:00 | | \_ /bin/bash /home/jenkins/workspace/build-1629@tmp/terraform.sh init
11380 ? S 0:00 | | \_ /usr/bin/coreutils --coreutils-prog-shebang=tee /usr/bin/tee -a /home/jenkins/workspace/build-1629/src/vnet-01/stderr.log
and after a few moments:
> ps aux
...
11377 ? Z 0:00 [terraform.sh] <defunct>
11378 ? Z 0:00 [terraform.sh] <defunct>
...
teesubshells are not collected even after the shell that launched them terminates? Or to put it another way: please be more specific about what you observe, and how you observe it.terraform-1.4binary (PID 11376), not the parent script (PID 11295).kubelet, I guess you are using Kubernetes. See this Kubernetes issue: github.com/kubernetes/kubernetes/issues/84210 for some discussion.