func kill(cmd *exec.Cmd) func() {
return func() {
if cmd != nil {
cmd.Process.Kill()
}
}
}
func main() {
cmd := exec.Command("/bin/bash", "-c", "watch top >top.log")
time.AfterFunc(1*time.Second, kill(cmd))
err := cmd.Run()
fmt.Printf("pid=%d err=%s\n", cmd.Process.Pid, err)
}
go run main.go
pid=27326 err=signal: killed
ps -j
USER PID PPID PGID SESS JOBC STAT TT TIME COMMAND
king 24324 1 24303 0 0 S s012 0:00.01 watch top
int kill(pid_t pid, int sig);
func kill(cmd *exec.Cmd) func() {
return func() {
if cmd != nil {
// cmd.Process.Kill()
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}
}
}
func main() {
cmd := exec.Command("/bin/bash", "-c", "watch top >top.log")
time.AfterFunc(1*time.Second, kill(cmd))
err := cmd.Run()
fmt.Printf("pid=%d err=%s\n", cmd.Process.Pid, err)
}
go run main.go
ps -j
USER PID PPID PGID SESS JOBC STAT TT TIME COMMAND
king 27655 91597 27655 0 1 S+ s012 0:01.10 go run main.go
king 27672 27655 27655 0 1 S+ s012 0:00.03 ..../exe/main
king 27673 27672 27655 0 1 S+ s012 0:00.00 /bin/bash -c watch top >top.log
king 27674 27673 27655 0 1 S+ s012 0:00.01 watch top
int setpgid(pid_t pid, pid_t pgid);
func kill(cmd *exec.Cmd) func() {
return func() {
if cmd != nil {
// cmd.Process.Kill()
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}
}
}
func main() {
cmd := exec.Command("/bin/bash", "-c", "watch top >top.log")
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
time.AfterFunc(1*time.Second, kill(cmd))
err := cmd.Run()
fmt.Printf("pid=%d err=%s\n", cmd.Process.Pid, err)
}
go run main.go
pid=29397 err=signal: killed
ps -j
USER PID PPID PGID SESS JOBC STAT TT TIME COMMAND
# 由于我测试的环境是mac,因此这个脚本只能在mac执行
ps -j | head -1;ps -j | awk '{if ($3 ==1 && $1 !="root"){print $0}}' | head
USER PID PPID PGID SESS JOBC STAT TT TIME COMMAND
int prctl(int option, unsigned long arg2, unsigned long arg3,
unsigned long arg4, unsigned long arg5);
package main
import (
"os/exec"
)
func main() {
cmd := exec.Command("./child")
cmd.Run()
}
package main
import (
"fmt"
"time"
)
func main() {
for {
time.Sleep(200 * time.Millisecond)
fmt.Println(time.Now())
}
}
// 编译 main.go 生成 main 二进制文件
go build -o main main.go
// 编译 child.go 生成 child 二进制文件
go build -o child child.go
./main &
ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 06:05 pts/0 00:00:00 /bin/bash
root 11514 1 0 12:12 pts/0 00:00:00 ./main
root 11520 11514 0 12:12 pts/0 00:00:00 ./child
kill -9 11514
ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 06:05 pts/0 00:00:00 /bin/bash
root 11520 1 0 12:12 pts/0 00:00:00 ./child
import (
"fmt"
"time"
)
// #include <stdio.h>
// #include <stdlib.h>
// #include <sys/prctl.h>
// #include <signal.h>
//
// static void killTest() {
// prctl(PR_SET_PDEATHSIG,SIGKILL);
// }
import "C"
func main() {
C.killTest()
for {
time.Sleep(200 * time.Millisecond)
fmt.Println(time.Now())
}
}
go build -o child child.go
&
ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 06:05 pts/0 00:00:00 /bin/bash
root 11663 1 0 12:28 pts/0 00:00:00 ./main
root 11669 11663 0 12:28 pts/0 00:00:00 ./child
kill -9 11663
ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 06:05 pts/0 00:00:00 /bin/bash
// 其他内容省略掉了
const(
....
PR_SET_PDEATHSIG = 0x1
....
)
const(
.....
SYS_PRCTL = 157
.....
)
package main
import (
"fmt"
"os"
"syscall"
"time"
)
func main() {
_, _, errno := syscall.RawSyscall(uintptr(syscall.SYS_PRCTL), uintptr(syscall.PR_SET_PDEATHSIG), uintptr(syscall.SIGKILL), 0)
if errno != 0 {
os.Exit(int(errno))
}
for {
time.Sleep(200 * time.Millisecond)
fmt.Println(time.Now())
}
}
go build -o child child.go
&
ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 06:05 pts/0 00:00:00 /bin/bash
root 12208 1 0 12:46 pts/0 00:00:00 ./main
root 12214 12208 0 12:46 pts/0 00:00:00 ./child
kill -9 12208
ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 06:05 pts/0 00:00:00 /bin/bash
给要执行的程序创建新的进程组,并调用 syscall.Kill,传递负值 pid 来关闭这个进程组中所有的进程(比较完美的解决方法)。
如果要调用的程序也是我们自己编写的,那么可以使用 PR_SET_PDEATHSIG 来感知父进程退出,那么这种方式需要调用 Linxu 的 prctrl,可以使用 CGO 的方式,也可以使用 syscall.RawSyscall 的方式。
技术公开课
《Go语言核心编程(2):面向对象、文件、单元测试、反射、TCP编程》
本课程共162课时,包含Go面向对象编程、文件操作、单元测试、goroutine和channel、反射、Redis、TCP编程等核心基础知识,以及家庭收支记账和客户管理系统实战。