Skip to content

Instantly share code, notes, and snippets.

@longkey1
Last active October 29, 2015 15:59
Show Gist options
  • Save longkey1/04a4536c5b57892a1d0f to your computer and use it in GitHub Desktop.
Save longkey1/04a4536c5b57892a1d0f to your computer and use it in GitHub Desktop.
go-routine-sample
package main
import (
"log"
"time"
"os"
"os/exec"
"syscall"
)
func main() {
log.Print("started.")
command()
// go command()
// i := 0
// for ;; {
// i++
//// log.Println(i)
// }
log.Print("all finished.")
}
func command() {
cmd := exec.Command(os.Getenv("SHELL"), "-c", "./sleep.sh")
cmd.Start();
pgid := getPgid(cmd.Process)
time.Sleep(10 * time.Second)
killPg(pgid)
}
func getPgid(ps *os.Process) (int) {
pgid, err := syscall.Getpgid(ps.Pid)
if err != nil {
panic("can not get pgid")
}
return pgid
}
func killPg(pgid int) {
err := syscall.Kill(-pgid, 15)
if err != nil {
panic("can not kill pg")
}
}
#!/usr/bin/env bash
sleep 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment