feat: signal command and wait before killing
1 file changed, 19 insertions(+), 1 deletion(-)
changed files
M command/command.go → command/command.go
@@ -7,6 +7,7 @@ "io" "os" "os/exec" "strings" + "time" "github.com/fatih/color" )@@ -26,6 +27,7 @@ opts Options printFail func(string, ...any) + printInfo func(string, ...any) printSuccess func(string, ...any) }@@ -34,6 +36,8 @@ Stdout io.Writer Stderr io.Writer } +const timeout = 1 * time.Second + func New(name string, args []string, options Options) *Cmd { if options.Stdout == nil { options.Stdout = os.Stdout@@ -48,15 +52,29 @@ args: args, opts: options, printFail: color.New(color.FgRed).PrintfFunc(), + printInfo: color.New().PrintfFunc(), printSuccess: color.New(color.FgGreen).PrintfFunc(), } } func (cmd *Cmd) Stop() error { if cmd.Cmd != nil && cmd.Process != nil && cmd.ProcessState == nil { - err := cmd.Process.Kill() + err := cmd.Process.Signal(os.Interrupt) if err != nil { return fmt.Errorf("error killing command: %v", err) + } + + if cmd.ProcessState == nil { + cmd.printInfo("[command not stopped, waiting %s seconds before killing]\n", timeout) + time.Sleep(timeout) + + if cmd.ProcessState == nil { + cmd.printInfo("[command not stopped, killing]\n") + err := cmd.Process.Kill() + if err != nil { + return fmt.Errorf("error killing command: %v", err) + } + } } }