如何从Node正确终止Go进程

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

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')