diff --git a/cmd/globals.go b/cmd/globals.go index 2066e8c2..4afffa49 100644 --- a/cmd/globals.go +++ b/cmd/globals.go @@ -42,6 +42,15 @@ const ( // Global error exit status. globalErrorExitStatus = 1 + + // Global CTRL-C (SIGINT, #2) exit status. + globalCancelExitStatus = 130 + + // Global SIGKILL (#9) exit status. + globalKillExitStatus = 137 + + // Global SIGTERM (#15) exit status + globalTerminatExitStatus = 143 ) var ( diff --git a/cmd/signals.go b/cmd/signals.go index 15954a1a..71801f7f 100644 --- a/cmd/signals.go +++ b/cmd/signals.go @@ -32,13 +32,24 @@ func trapSignals(sig ...os.Signal) { signal.Notify(sigCh, sig...) // Wait for the signal. - <-sigCh + s := <-sigCh // Once signal has been received stop signal Notify handler. - signal.Stop(sigCh) // Cancel the global context globalCancel() + var exitCode int + switch s.String() { + case "interrupt": + exitCode = globalCancelExitStatus + case "killed": + exitCode = globalKillExitStatus + case "terminated": + exitCode = globalTerminatExitStatus + default: + exitCode = globalErrorExitStatus + } + os.Exit(exitCode) }