设为首页 加入收藏

TOP

GoLang 的 daemonize 实现
2017-09-30 13:45:54 】 浏览:4509
Tags:GoLang daemonize 实现
func daemonize(cmd string, args []string, pipe io.WriteCloser) error {
	pid, _, sysErr := syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0)
	if sysErr != 0 {
		return fmt.Errorf("fail to call fork")
	}
	if pid > 0 {
		if _, err := syscall.Wait4(int(pid), nil, 0, nil); err != nil {
			return fmt.Errorf("fail to wait for child process: %v", err)
		}
		return nil
	} else if pid < 0 {
		return fmt.Errorf("child id is incorrect")
	}

	ret, err := syscall.Setsid()
	if err != nil || ret < 0 {
		return fmt.Errorf("fail to call setsid")
	}

	signal.Ignore(syscall.SIGHUP)
	syscall.Umask(0)

	nullFile, err := os.Open(os.DevNull)
	if err != nil {
		return fmt.Errorf("fail to open os.DevNull: %v", err)
	}
	files := []*os.File{
		nullFile, // (0) stdin
		nullFile, // (1) stdout
		nullFile, // (2) stderr
	}
	attr := &os.ProcAttr{
		Dir:   "/",
		Env:   os.Environ(),
		Files: files,
	}
	child, err := os.StartProcess(cmd, args, attr)
	if err != nil {
		return fmt.Errorf("fail to start process: %v", err)
	}

	buff := make([]byte, 4)
	binary.BigEndian.PutUint32(buff[:], uint32(child.Pid))
	if n, err := pipe.Write(buff); err != nil || n != 4 {
		return fmt.Errorf("fail to write back the pid")
	}

	os.Exit(0)
	return nil
}

  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Install gocode 下一篇win7环境下安装运行gotour【转载..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目