Created
November 8, 2016 22:10
-
-
Save lwalen/6dc1d43ec7fe4c8fa5d4509099697b1d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// Catches the SIGTERM given by ^C and waits until the end of the loop to quit. | |
import ( | |
"fmt" | |
"os" | |
"os/signal" | |
"syscall" | |
"time" | |
) | |
func runTasks(c chan os.Signal) { | |
for { | |
fmt.Println("starting") | |
time.Sleep(time.Second * 3) | |
fmt.Println("ending") | |
select { | |
case <-c: | |
fmt.Println("safe exit!") | |
os.Exit(2) | |
default: | |
} | |
} | |
} | |
func main() { | |
c := make(chan os.Signal, 2) | |
signal.Notify(c, os.Interrupt, syscall.SIGTERM) | |
runTasks(c) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment