英文:
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论