Closed
Description
Initially opened here: hashicorp/terraform-aws-vault#153
Recently learned about traps and catching different signals and have been implementing them wherever I can in my Bash scripts.
After reading https://github.com/hashicorp/terraform-aws-vault/blob/master/test/README.md they came into mind in regards to cutting the tests short and the environment not properly cleaning up.
For go usage see: https://stackoverflow.com/questions/11268943/is-it-possible-to-capture-a-ctrlc-signal-and-run-a-cleanup-function-in-a-defe
example trap:
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time" // or "runtime"
)
func cleanup() {
fmt.Println("cleanup")
}
func main() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
cleanup()
os.Exit(1)
}()
for {
fmt.Println("sleeping...")
time.Sleep(10 * time.Second) // or runtime.Gosched() or similar per @misterbee
}
}