Skip to content

Instantly share code, notes, and snippets.

@maierru
Created January 19, 2018 15:32
Show Gist options
  • Save maierru/345af09cd43b86418686edb986eb79a5 to your computer and use it in GitHub Desktop.
Save maierru/345af09cd43b86418686edb986eb79a5 to your computer and use it in GitHub Desktop.
go systems signals handler
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
func sygnalsWait() {
pid := os.Getpid()
fmt.Printf("pid: %v", pid)
SigChannel := make(chan os.Signal, 1)
done := make(chan int, 1)
// kill pid
signal.Notify(SigChannel, syscall.SIGTERM)
// kill -s USR1 pid
signal.Notify(SigChannel, syscall.SIGUSR1)
// kill -s USR2
signal.Notify(SigChannel, syscall.SIGUSR2)
go func() {
for {
s := <-SigChannel
switch s {
case syscall.SIGUSR1:
fmt.Printf("\nReceived signal %v", s)
case syscall.SIGUSR2:
fmt.Printf("\nReceived signal %v", s)
default:
fmt.Printf("\nExiting %v after 3s", s)
time.Sleep(3 * time.Second)
done <- 0
}
}
}()
<-done
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment