Skip to content

Instantly share code, notes, and snippets.

@tchap
Last active August 29, 2015 14:06
Show Gist options
  • Save tchap/dd034e67a50af84dc443 to your computer and use it in GitHub Desktop.
Save tchap/dd034e67a50af84dc443 to your computer and use it in GitHub Desktop.
// The following code works fine:
//
// $ ./signals-test
// ^CSignal received, handler unregistered...
// ^C
// $
package main
import (
"fmt"
"os"
"os/signal"
"time"
)
func main() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
go catchSignals(ch)
time.Sleep(60 * time.Second)
}
func catchSignals(ch chan os.Signal) {
<-ch
fmt.Println("Signal received, handler unregistered...")
signal.Stop(ch)
}
// The following code works in a strange way, the prompt appears immediately:
//
// $ ./signals-test
// ^CSignal received, handler unregistered...
// $
//
// When I run ps now, there is no signals-test process.
// Looks like the difference is the spawning of a new process, dunno why...
package main
import (
"fmt"
"os"
"os/exec"
"os/signal"
)
func main() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
go catchSignals(ch)
cmd := exec.Command("sleep", "60")
cmd.Run()
}
func catchSignals(ch chan os.Signal) {
<-ch
fmt.Println("Signal received, handler unregistered...")
signal.Stop(ch)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment