Last active
August 26, 2021 16:01
-
-
Save djoreilly/f82c2f29bb0dc501f6b957d705037404 to your computer and use it in GitHub Desktop.
Revisions
-
djoreilly revised this gist
Aug 26, 2021 . No changes.There are no files selected for viewing
-
djoreilly created this gist
Aug 26, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ package main import ( "fmt" "os" "os/exec" "os/signal" "syscall" "time" ) func interrupted(sigCh <-chan os.Signal) bool { // non-blocking read on channel select { case <-sigCh: default: return false // channel is empty } return true } func main() { sigCh := make(chan os.Signal, 1) signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) fmt.Println("about to sleep") startTime := time.Now() cmd := exec.Command("sleep", "5") // prevent the shell from signalling children https://stackoverflow.com/a/33171307 cmd.SysProcAttr = &syscall.SysProcAttr{ Setpgid: true, } cmd.Run() fmt.Printf("slept for %0.0f sec\n", time.Now().Sub(startTime).Seconds()) if interrupted(sigCh) { fmt.Println("interrupted") } else { fmt.Println("not interrupted") } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ $ go run interrupted.go about to sleep ^C^C^C slept for 5 sec interrupted $ go run interrupted.go about to sleep slept for 5 sec not interrupted