英文:
Send signal to other process
问题
os/signal
模块只用于处理传入信号,我想知道是否有一种原生的方法可以向其他进程发送信号?
编辑:我想要管理的进程是nginx。我的Go应用程序应该监听某些事件,然后向nginx发送SIGHUP
信号以重新加载其配置。
英文:
since os/signal
is only for handling of incoming signals I'm asking if there is a native way to send signals to other processes?
Edit: The process I want to mange is nginx. My Go application should listen for some events and then send a SIGHUP
to nginx to reload its configuration
答案1
得分: 7
如果你使用os.StartProcess
或exec.Command
创建了一个进程,你可以使用Process.Signal
方法:
cmd := exec.Command("CmdPath", "-param1", param1)
cmd.Process.Signal(signal)
对于外部进程,你需要知道它的进程ID(PID),然后调用类似以下的方法:
syscall.Kill(pid, syscall.SIGHUP)
Signal
是一个标准的UNIX "kill"系统调用,如果你知道进程的PID,你可以简单地使用syscall
包进行系统调用。
英文:
If you have created process with os.StartProcess or exec.Command you can use Process.Signal method:
cmd := exec.Command("CmdPath", "-param1", param1)
cmd.Process.Signal(signal)
For external process You should know it's process id (PID) and then call for example:
syscall.Kill(pid,syscall.SIGHUP)
Signal is a standart UNIX "kill" syscall, if you know a pid of the process, you can simply make a syscall with
https://golang.org/pkg/syscall/#Kill
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论