Skip to content

Instantly share code, notes, and snippets.

@felixge
Last active March 23, 2017 07:52
Show Gist options
  • Save felixge/c34e11269838dc169376 to your computer and use it in GitHub Desktop.
Save felixge/c34e11269838dc169376 to your computer and use it in GitHub Desktop.
cmd := exec.Command("/bin/sh", "-c", "watch date > date.txt")
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
start := time.Now()
time.AfterFunc(3*time.Second, func() {
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
})
err := cmd.Run()
fmt.Printf("pid=%d duration=%s err=%s\n", cmd.Process.Pid, time.Since(start), err)
$ go run example3.go
pid=41352 duration=3.001809822s err=signal: killed
$ ps -j
USER PID PPID PGID SESS JOBC STAT TT TIME COMMAND
felix 41008 41002 41008 0 0 Ss s000 0:00.14 -/bin/bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment