Skip to content

Commit 3749aaa

Browse files
committed
Cancel process on interrupt
1 parent 50c6508 commit 3749aaa

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

cmd/godotenv/cmd.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"log"
@@ -54,12 +55,16 @@ example
5455
cmd := args[0]
5556
cmdArgs := args[1:]
5657

57-
command := exec.Command(cmd, cmdArgs...)
58+
ctx, cancel := context.WithCancel(context.Background())
59+
notify := make(chan os.Signal, 1)
60+
signal.Notify(notify, os.Interrupt)
61+
go func() { <-notify; cancel() }()
62+
63+
command := exec.CommandContext(ctx, cmd, cmdArgs...)
5864
command.Stdin = os.Stdin
5965
command.Stdout = os.Stdout
6066
command.Stderr = os.Stderr
6167

62-
signal.Ignore(os.Interrupt)
6368
if err := command.Run(); err != nil {
6469
log.Fatal(err)
6570
}

0 commit comments

Comments
 (0)