Catching "stop" in Go
Greetings
Is there a way to catch the forced end of a Go program being debugged in Goland? I've tried to catch SIGTERM, SIGINT, SIGQUIT, SIGKILL, SIGSTOP and SIGHUP but none seems to be called...
Thanks
Please sign in to leave a comment.
It should work but might depends on the OS you are using. On linux the following program works:
package main
import (
"fmt"
"os"
"os/signal"
"time"
)
func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
sig := <- sigs
fmt.Printf("Got a signal '%v', start shutting down...\n", sig) // put breakpoint here
time.Sleep(3 * time.Second)
fmt.Println("Shutdown complete")
}
If you send a breakpoint from console (kill -2 <pid>).
Thanks for your answer.
I'm using something similar to what you wrote. The problem is, when I use the "stop and rerun" function from the IDE, it's not caught :(
At the moment, IDE sends os.Kill signal which cannot be caught. Please watch/vote for https://youtrack.jetbrains.com/issue/GO-5982 to get a notification once it is fixed.