golang handling kill in a process started by cmd.Start

Issue

I have two go programs. ProgA starts ProgB using cmd.Start(). From ProgA I try to kill ProgB, but ProgB shouldn’t get killed immediately, it has to do some cleanup before dying. So I’m using signal.Notify in ProgB to handle sigcall.SIGKILL but whenever ProgA calls progb.Process.Kill() it doesn’t seem to notify ProgB(write contents to sigc channel)

in ProgB I have the notify like this:

signal.Notify(sigc, syscall.SIGKILL)
go func() {
            fmt.Println("started listening")
            <-sigc
            fmt.Println("sig term")
            cleanUp()
            os.Exit(1)   
            
}()
someLongRunningCode() 

is there something I’m missing out? I’m sure that ProgA sends a SIGKILL because cmd.Process.Kill() internally does a process.Signal(SIGKILL)

Solution

SIGKILL cannot be trapped by recieving process – kernel will force process termination. You may send SIGTERM to process and handle it on other side – it is a conventional method to stop an application.

Answered By – mechmind

Answer Checked By – Terry (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.