Skip to content

Commit

Permalink
Cancel process on interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSLevy committed Sep 14, 2020
1 parent 50c6508 commit 3749aaa
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cmd/godotenv/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -54,12 +55,16 @@ example
cmd := args[0]
cmdArgs := args[1:]

command := exec.Command(cmd, cmdArgs...)
ctx, cancel := context.WithCancel(context.Background())
notify := make(chan os.Signal, 1)
signal.Notify(notify, os.Interrupt)
go func() { <-notify; cancel() }()

command := exec.CommandContext(ctx, cmd, cmdArgs...)
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr

signal.Ignore(os.Interrupt)
if err := command.Run(); err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 3749aaa

Please sign in to comment.