如何从Node中正确终止一个Go进程

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

How to properly kill a Go process from Node

问题

我想要构建一个Grunt任务,当Go源文件发生变化时,它会生成一个Go服务器,然后杀死并重新生成它。

我是这样生成Go进程的:

goProcess = child_process.exec('go run main.go', ...

然后我尝试这样杀死进程:

if (goProcess) {
	goProcess.kill('SIGINT');
}

但是Go进程没有被杀死。

我应该如何在Node中正确地杀死Go进程?

这里有一个可行的示例:https://github.com/sporto/go-must-die

英文:

I want to build a Grunt task that spawns a Go server and then kills and respawns it when the Go source files change.

I am spawning the Go process like this:

goProcess = child_process.exec('go run main.go', ...

Later I'm trying to kill the process like this:

if (goProcess) {
	goProcess.kill('SIGINT');
}

But the Go process doesn't die.

How can I properly kill the Go process within Node?

I have a working example here https://github.com/sporto/go-must-die

答案1

得分: 0

child_process.exec 在一个 shell 中运行命令,并似乎返回了 shell 进程的 PID。请改用 spawn

go run 创建一个可执行文件,并使用不同的 PID 运行它。尝试使用 go build main.go 构建一个二进制文件,并从 node.js 中运行该二进制文件。

goProcess = child_process.spawn('./main');
英文:

child_process.exec runs the command in a shell and seems to be returning the PID of the shell process. Use spawn instead.

go run creates an executable and runs it with a different PID. Try building a binary with go build main.go and running the binary from node.js.

goProcess = child_process.spawn('./main')

huangapple
  • 本文由 发表于 2013年10月16日 12:08:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/19395070.html
匿名

发表评论

匿名网友

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

确定