Process.Kill()在子进程上 [英] Process.Kill() on child processes
问题描述
我试图停止以 exec.Command(go,run,server.go)
及其所有子进程开始的进程。
但当我调用 cmd.Process.Kill()
和去 code>进程停止,子进程(位于
server.go
中的web服务器)继续运行。
package main
import(
fmt
os / exec
time
)
func run()* exec.Cmd {
cmd:= exec.Command(go,run,server.go)
err:= cmd.Start()
if err!= nil {
panic(err)
}
return cmd
}
func main(){
cmd:= run()
time.Sleep(time.Second * 2)
err:= cmd .Process.Kill()
if err!= nil {
panic(err)
}
cmd.Process.Wait()
// === Web服务器仍在运行! ===
fmt.Scanln()
}
它看起来像 Process.Kill()
只会停止 go(run)
进程,但会留下子进程(web服务器)运行。
^ C
会杀死整个进程组,包括所有子进程(和子进程)。我怎么能这样做?
我试过 cmd.Process.Signal(os.Interrupt)
, syscall.SIGINT
, syscall.SIGQUIT
和 syscall.SIGKILL
,none 命令。使用 go install
命令来安装软件包和程序,然后执行你的程序。
I'm trying to stop the process started with exec.Command("go", "run", "server.go")
and all its child processes.
But when I call cmd.Process.Kill()
and the go
process stops, the child process (a web server in server.go
) continues to run.
package main
import (
"fmt"
"os/exec"
"time"
)
func run() *exec.Cmd {
cmd := exec.Command("go", "run", "server.go")
err := cmd.Start()
if err != nil {
panic(err)
}
return cmd
}
func main() {
cmd := run()
time.Sleep(time.Second * 2)
err := cmd.Process.Kill()
if err != nil {
panic(err)
}
cmd.Process.Wait()
// === Web server is still running! ===
fmt.Scanln()
}
It looks like Process.Kill()
only stops the go (run)
process, but leaves its child process (web server) running.
^C
kills the whole process group, including all child (and sub-child) processes. How can I do the same?
I tried cmd.Process.Signal(os.Interrupt)
, syscall.SIGINT
, syscall.SIGQUIT
and syscall.SIGKILL
, none of which did anything.
Don't use the go run
command. Use the go install
command to install your packages and programs and then execute your program.
这篇关于Process.Kill()在子进程上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!