How to send interrupt signal to golang?

I am currently trying to implement a function that will trigger an interrupt signal in Google Go. I know how to intercept interrupt signals from the console using signal.Notify(interruptChannel, os.Interrupt), however, I cannot find a way to actually send interrupt signals. I found that you can send a signal to a process , but I'm not sure if this can be used to send a top level interrupt signal.

Is there a way to send an interrupt signal from a golang function that might be caught by something that listens for system interrupt signals, or is it something that is not supported in golang?

+4
source share
2 answers

Get a process using FindProcess , StartProcess, or some other means. Call Signal to send an interrupt:

 err := p.Signal(os.Interrupt)

This will send a signal to the target process (provided that the calling process has permission to do so) and will call any signal handlers that the target process may have for SIGINT.

+7
source

Assuming you use something like this to capture an interrupt signal

var stopChan = make(chan os.Signal, 2)
signal.Notify(stopChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)

<-stopChan // wait for SIGINT

Use below anywhere in your code to send an interrupt to the next part of the wait.

syscall.Kill(syscall.Getpid(), syscall.SIGINT)

Or if you are in the same package where the stopChan variable is defined. Thus, it becomes available. You can do it.

stopChan <- syscall.SIGINT

stopChan ( ), .

stopChan <- syscall.SIGINT
+5

Source: https://habr.com/ru/post/1660244/


All Articles