From ef59238007df40f784a5040a5f3e08f3f71c1787 Mon Sep 17 00:00:00 2001 From: ebozduman Date: Thu, 15 Apr 2021 16:58:49 -0700 Subject: [PATCH] Adds error exit codes for SIGINT, SIGTERM and SIGKILL (#3662) --- cmd/globals.go | 9 +++++++++ cmd/signals.go | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) 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) }