I am forking into "daemon" mode with this function:
func daemon(nochdir, noclose int) int {
ret, _, err := syscall.Syscall(syscall.SYS_FORK, 0, 0, 0)
if err != 0 {
maybe_exit_err("Forking", err)
return -1
}
switch ret {
case 0:
break
default:
os.Exit(0)
}
sid, err2 := syscall.Setsid()
maybe_exit_err(fmt.Sprintf("could not set session: %s", sid), err2)
if sid == -1 {
return -1
}
if nochdir == 0 {
os.Chdir("/")
}
if noclose == 0 {
f, e := os.OpenFile("/dev/null", os.O_RDWR, 0)
if e == nil {
fd := int(f.Fd())
syscall.Dup2(fd, int(os.Stdin.Fd()))
syscall.Dup2(fd, int(os.Stdout.Fd()))
syscall.Dup2(fd, int(os.Stderr.Fd()))
}
}
return 0
}
The program works fine as long as I do not use time.Sleep(1 * time.Second)
. After that, it just sits there doing nothing, I have even added writing test string in a loop to a file I opened myself, and nothing is being written there after time.Sleep
is used (and it works fine if not).
What might be the reason here?
OS: Debian Linux 7.2 x64 go version: 1.2