diff --git a/README.md b/README.md index a354f22..6aade12 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,5 @@ Main features: failed state. - is about **200 loc** simple to copy and adapt to your custom needs. +Configuration: +- By default applications are killed using the `TERM` signal. This can be configured to the `INT` signal by setting the `GOUP_TERM_SIGNAL` environment variable to either `TERM` or `INT`. diff --git a/goup.go b/goup.go index 2e7b785..fd65976 100644 --- a/goup.go +++ b/goup.go @@ -5,6 +5,7 @@ package main import ( "bytes" "encoding/json" + "fmt" "go/build" "io" "io/ioutil" @@ -23,6 +24,8 @@ var logger = log.New(os.Stderr, "goup -> ", 0) var watchOps = []fsnotify.Op{fsnotify.Write, fsnotify.Create, fsnotify.Remove} var watchExt = []string{".go"} +var termSignal = os.Getenv("GOUP_TERM_SIGNAL") + type project struct { Name string Deps []string @@ -41,6 +44,10 @@ func main() { logger.Fatalf("failed import: %v", err) } + // Ensure term-signal flag is valid + getTermSignal() + logger.Printf("using termination signal: %s", termSignal) + c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { @@ -87,11 +94,22 @@ func main() { <-done } +// Get the signal used to terminal signals. +func getTermSignal() syscall.Signal { + if termSignal == "INT" { + return syscall.SIGINT + } else if termSignal == "TERM" || termSignal == "" { + return syscall.SIGTERM + } else { + panic(fmt.Sprintf("invalid GOUP_TERM_SIGNAL value: %s", termSignal)) + } +} + func (p *project) terminate() { // send termination signal, to running application if p.cmd != nil && p.cmd.Process != nil { logger.Println("terminating process:", p.cmd.Process.Pid) - if err := p.cmd.Process.Signal(syscall.SIGTERM); err != nil { + if err := p.cmd.Process.Signal(getTermSignal()); err != nil { logger.Println("failed to terminate:", p.cmd.Process.Pid, "reason:", err) } }