linux go守护进程,Go到目前还没有解决成为守护进程(Daemonize)的问题吧?各位是怎么解决的?...

这是StackOverFlow上写的,

还有官网的

我自己试图抄了一段代码,可以工作,但问题在于我在子进程里面调用了一下time.Sleep后,子进程就不动了。用strace看到子进程卡在epoll调用上。

请问大家是怎么解决这个问题的?就用很土的nohup ./go-exec &这种shell来实现吗?

package main

import (

"log"

"os"

"runtime"

"errors"

"syscall"

)

func daemon(chdir bool, closeStd bool) (err error){

var ret, ret2 uintptr

var er syscall.Errno

darwin := runtime.GOOS == "darwin"

// already a daemon

if syscall.Getppid() == 1 {

return

}

// fork off the parent process

ret, ret2, er = syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0)

if er != 0 {

err = errors.New("fork fail")

return

}

// failure

if ret2 < 0 {

err = errors.New("fork fail")

os.Exit(-1)

}

// handle exception for darwin

if darwin && ret2 == 1 {

ret = 0

}

// if we got a good PID, then we call exit the parent process.

if ret > 0 {

os.Exit(0)

}

/* Change the file mode mask */

_ = syscall.Umask(0)

// create a new SID for the child process

s_ret, s_errno := syscall.Setsid()

if s_errno != nil {

log.Printf("Error: syscall.Setsid errno: %d", s_errno)

}

if s_ret < 0 {

err = errors.New("setsid fail")

return

}

if chdir {

os.Chdir("/")

}

if closeStd {

f, e := os.OpenFile("/dev/null", os.O_RDWR, 0)

if e == nil {

fd := f.Fd()

syscall.Dup2(int(fd), int(os.Stdin.Fd()))

syscall.Dup2(int(fd), int(os.Stdout.Fd()))

syscall.Dup2(int(fd), int(os.Stderr.Fd()))

}

}

return

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值