确保Process处理被终止时,确保Go Process中调用的可执行文件被杀死 [英] Ensure executables called in Go Process get killed when Process is killed

查看:311
本文介绍了确保Process处理被终止时,确保Go Process中调用的可执行文件被杀死的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Go(golang)中的代码,它有几个不同的线程运行单独的可执行文件。我想确保如果用户在Go中杀死了我的进程,那么我有一种杀死我调用的可执行文件的方法,有没有办法做到这一点?

解决方案

确保子进程被终止的唯一方法是在同一个进程组中启动它,并将进程组作为一个整体终止,或将 Pdeadthsig syscall.SetProcAddr

您可以为常见信号设置信号处理程序,如 SIG_INT SIG_TERM ,并且在退出之前终止你的子进程,但是因为你不能捕获 SIG_KILL ,这通常是不值得的。 / p>

请参阅:恐慌在其他goroutine不停止子进程

  cmd:= exec.Command(./罗ng-process)

cmd.SysProcAttr =& syscall.SysProcAttr {
Pdeathsig:syscall.SIGTERM,
}


I have some code that in Go (golang), has a few different threads running a separate executable. I want to ensure that if a user kills my process in Go, that I have a way of killing the executables I've called, is there a way to do that?

解决方案

The only ways to ensure that the child process is killed, is to start it in the same process group, and kill the process group as a whole, or set Pdeadthsig in the syscall.SetProcAddr.

You can setup a signal handler for common signals like SIG_INT and SIG_TERM, and kill your child processes before exiting, but since you can't catch SIG_KILL that's often not worth the effort.

See: Panic in other goroutine not stopping child process

cmd := exec.Command("./long-process")

cmd.SysProcAttr = &syscall.SysProcAttr{
    Pdeathsig: syscall.SIGTERM,
}

这篇关于确保Process处理被终止时,确保Go Process中调用的可执行文件被杀死的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆