golang 在正在运行的二进制文件/进程上执行命令

标签 go operating-system signals

如果您查看 Nginx,它会调用“nginx reload”来重新加载自身。有什么方法可以从命令行向正在运行的进程发送信号吗?即使主进程启动了子进程,我如何向主进程发送命令以通知其子进程?

例如:

myapp start -debug // starts a server
myapp reload -gracefull // stops the app gracefully

现在我需要发送操作系统信号来通知我的服务器执行正常关机

kill -QUIT pid
kill -USR2 pid

我希望我的问题足够清楚 谢谢

最佳答案

接收信号

看看 os/signal包。

Package signal implements access to incoming signals.

甚至还有一个 example在文档中:

// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)

// Block until a signal is received.
s := <-c
fmt.Println("Got signal:", s)

发送信号

要了解如何发送信号,请查看 signal_test.go , 它使用 syscall .例如:

// Send this process a SIGHUP
t.Logf("sighup...")
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
waitSig(t, c, syscall.SIGHUP)

关于golang 在正在运行的二进制文件/进程上执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25896520/

相关文章:

c++ - 错误错误: ‘void*’ is not a pointer-to-object type

linux - 什么是零信号?

c++ - Qt QTcpSocket 读取数据时不发出信号

go - Go中的二级缓存(内存+redis)实现

go - 解码yaml

compiler-construction - if-else undefined variable 编译错误

math - Golang 中的精度

c - 分页代码片段

c - 在 C 中,如何在 Linux 中获得完整的时间戳(甚至在几秒钟之后)?

c - 为什么会有SIGFPE?