Created
May 12, 2014 20:30
-
-
Save mmcdaris/e5d64732ccd1a3b83854 to your computer and use it in GitHub Desktop.
handles signals, don't send -9 ;P
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 ( | |
"fmt" | |
"os" | |
"os/signal" | |
"syscall" | |
) | |
func main() { | |
// Set up a channel on which to send signal notifications | |
// We must use a buffered channel or risk missing the signal | |
// If we are not ready to receive when the signal is sent | |
// c is a channel that will hold 1 os.signal | |
c := make(chan os.Signal, 1) | |
// relay incoming signals to c | |
// if not signals are listed all signals wil | |
signal.Notify(c) | |
// block until a signal is received | |
for i := 0; i < 10; i++ { | |
s := <-c | |
// to send the INFO signal one just do | |
// kill -INFO $(pgrep signals) | |
if s == syscall.SIGINFO { | |
fmt.Printf("%s counter decrimented!\n", s) | |
if i > 1 { | |
i = i - 2 | |
} | |
fmt.Println("counter:", i) | |
} else { | |
fmt.Println("signal:", s, "counter:", i) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment