Skip to content

Instantly share code, notes, and snippets.

@lwalen
Created November 8, 2016 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lwalen/6dc1d43ec7fe4c8fa5d4509099697b1d to your computer and use it in GitHub Desktop.
Save lwalen/6dc1d43ec7fe4c8fa5d4509099697b1d to your computer and use it in GitHub Desktop.
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