From 3ea6d32c65d21018b81f66c129fac6d62b8b8e30 Mon Sep 17 00:00:00 2001 From: Dave Gallant Date: Sun, 3 Jan 2021 16:04:51 -0500 Subject: [PATCH] Ensure clean exit of exec.Command --- .gitignore | 1 + pkg/exec/run.go | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..849ddff --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +dist/ diff --git a/pkg/exec/run.go b/pkg/exec/run.go index 22cc9ae..adadb1e 100644 --- a/pkg/exec/run.go +++ b/pkg/exec/run.go @@ -5,12 +5,15 @@ import ( "os" "os/exec" "strings" + "syscall" "github.com/juju/errors" "github.com/rs/zerolog/log" ) -// Run executes anycommand in workDir and returns stdout and error. +// Run executes a command in workDir and returns stdout and error. +// The spawned process will exit upon termination of this application +// to ensure a clean exit func Run(path string, workDir string, args ...string) (string, error) { _, err := exec.LookPath(path) if err != nil { @@ -23,6 +26,9 @@ func Run(path string, workDir string, args ...string) (string, error) { stderr := &bytes.Buffer{} cmd.Stdout = stdout cmd.Stderr = stderr + cmd.SysProcAttr = &syscall.SysProcAttr{ + Pdeathsig: syscall.SIGTERM, + } log.Debug().Msgf("Executing " + strings.Join(cmd.Args, " ")) err = cmd.Run() output := strings.TrimSpace(stdout.String())