0

I'm running Terraform from a script in a pipeline. I would like to ensure that if the pipeline job is aborted, Terraform receives SIGTERM.

_term() {
  tf_pid="$(pidof terraform)"
  kill -TERM ${tf_pid}
  wait ${tf_pid}
}

trap _term SIGTERM
...
...
...
terraform "$@"

With this approach, SIGTERM is not propagated. However, it is propagated if I call terraform this way in my script:

terraform "$@" & wait $!

Why is that?

Bernard Halas
  • 972
  • 11
  • 24
  • I need to mention, that I'm not happy with the solution, because I plan to pipe the process output. And if the output is piped, then `$!` doesn't capture the `terraform` PID, instead, it captures the PID of the last process in the pipe. – Bernard Halas Mar 17 '23 at 07:22
  • Closing as I found the answer here: https://stackoverflow.com/questions/46525435/how-to-immediately-trap-a-signal-to-an-interactive-bash-shell – Bernard Halas Mar 17 '23 at 09:17

0 Answers0