mirror of
https://github.com/davegallant/vpngate.git
synced 2025-08-07 09:02:31 +00:00
Ensure clean exit of exec.Command
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
dist/
|
@@ -5,12 +5,15 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
"github.com/rs/zerolog/log"
|
"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) {
|
func Run(path string, workDir string, args ...string) (string, error) {
|
||||||
_, err := exec.LookPath(path)
|
_, err := exec.LookPath(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -23,6 +26,9 @@ func Run(path string, workDir string, args ...string) (string, error) {
|
|||||||
stderr := &bytes.Buffer{}
|
stderr := &bytes.Buffer{}
|
||||||
cmd.Stdout = stdout
|
cmd.Stdout = stdout
|
||||||
cmd.Stderr = stderr
|
cmd.Stderr = stderr
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||||
|
Pdeathsig: syscall.SIGTERM,
|
||||||
|
}
|
||||||
log.Debug().Msgf("Executing " + strings.Join(cmd.Args, " "))
|
log.Debug().Msgf("Executing " + strings.Join(cmd.Args, " "))
|
||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
output := strings.TrimSpace(stdout.String())
|
output := strings.TrimSpace(stdout.String())
|
||||||
|
Reference in New Issue
Block a user