sevlyar / go-daemon

A library for writing system daemons in golang.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

enable setting pgid

chensk opened this issue · comments

After reborn, the new process get pgid the same as it's process id by default. I need to be able to customize the pgid so that I could kill it nicely since the reborn process becomes an orphan process.

By the way, directly calling to syscall.Setpgid would fail: operation not permitted

pgid, _ := syscall.Getpgid(os.Getpid())
cntxt := &daemon.Context{
	Umask: 027,
	Env:   append(os.Environ(), "PGID="+strconv.Itoa(pgid)),
}

d, _ := cntxt.Reborn()
defer cntxt.Release()

for _, k := range os.Environ() {
        if strings.HasPrefix(k, "PGID=") {
	        pgid, _ := strconv.Atoi(strings.Split(k, "=")[1])
	        if err := syscall.Setpgid(os.Getpid(), pgid); err != nil {
		        panic("fail to set pgid")
	        }
        }
}

I'm wondering if there is any way to achieve what I need?