发送信号给其他进程

huangapple go评论98阅读模式
英文:

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.StartProcessexec.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包进行系统调用。

Process.Signal

英文:

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

huangapple
  • 本文由 发表于 2015年12月15日 17:29:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/34285445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定