linux - 父进程死亡时自动杀死子进程

标签 linux go

Golang中父进程死亡时,如何自动杀死子进程

子进程由exec.Command()调用。

例如,

父进程pid为:28290,有3个子进程:32062、32473、33455。

# ps axo pid、ppid、pgid | grep 28290

PID PPID PGID

28290 1 28289

32062 28290 28289

32473 28290 28289

33455 28290 28289

四个进程具有相同的PGID=28289。

当我杀死父进程28290(在linux中杀死28290,kill -9 28290)时,子进程不会退出()。

是否有任何选项可以自动终止子进程?

最佳答案

运行命令前设置进程组ID,并通过-pid杀死,注意减号。

// set pgid so all child processes can be killed together
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
...
// kill -pgid (-pid)
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)

关于linux - 父进程死亡时自动杀死子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45559019/

相关文章:

.net - 仅使用 .NET Core 2.0 运行时的 Linux 上缺少运行时存储错误

linux - 高级 unix 命令行技巧

c - 如果在文件范围内定义了签名缓冲区,为什么 public_key_verify_signature() 会返回错误? (Linux 内核加密)

go - 如何在 'Local'中使用 'GoLang'模块

linux - 使用硬件 token 登录

string - 可以在Go中的另一个字符串中嵌入一些值吗?

http - 新手编译报错net/http响应

go - Golang RPC调用解决意外的EOF

go - 如何在结构初始化器中使用指针类型?

c - 如何从 C 程序和 Ubuntu 操作系统读取远程设备的文件?