Created
September 23, 2017 05:34
-
-
Save catatsuy/d6259e08b129041053b2eba0d9544db3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"os" | |
"os/signal" | |
"syscall" | |
"time" | |
) | |
func main() { | |
var ( | |
survivalTime time.Duration | |
waitTime time.Duration | |
exitCode int | |
) | |
flag.DurationVar(&survivalTime, "survival-time", 0, "survival time") | |
flag.DurationVar(&waitTime, "wait-time", 0, "time to wait for a signal to be sent") | |
flag.IntVar(&exitCode, "exit-code", 0, "exit code") | |
flag.Parse() | |
sigchan := make(chan os.Signal) | |
signal.Notify(sigchan, syscall.SIGTERM) | |
signal.Notify(sigchan, syscall.SIGINT) | |
if survivalTime > 0 { | |
select { | |
case <-time.After(survivalTime): | |
break | |
case <-sigchan: | |
time.Sleep(waitTime) | |
break | |
} | |
} else if waitTime > 0 { | |
select { | |
case <-sigchan: | |
time.Sleep(waitTime) | |
break | |
} | |
} else { | |
} | |
os.Exit(exitCode) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment