workaround weird behavior of ctx.Release, pidfile now containts daemon's pid

This commit is contained in:
mykola2312 2025-01-25 02:16:31 +02:00
parent 2d7625101f
commit a1bad13490

15
main.go
View file

@ -130,7 +130,7 @@ func becomeDaemon() {
PidFileName: pidPath,
PidFilePerm: 0644,
WorkDir: cwd,
Umask: 027,
Umask: 002,
}
dmn, err := ctx.Reborn()
@ -139,22 +139,26 @@ func becomeDaemon() {
os.Exit(1)
}
if dmn != nil {
ctx.Release()
// parent process no longer needed
os.Exit(0)
}
defer ctx.Release()
// -- WE ARE NOW THE DAEMON --
// write pid
if pidPath != "" {
pidFile, _ := os.OpenFile(pidPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
pidFile, err := os.OpenFile(pidPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(0644))
if err != nil {
log.Errorf("failed to create pidfile: %v", err)
}
defer pidFile.Close()
pidBytes := []byte(strconv.Itoa(os.Getpid()))
pidFile.Write(pidBytes)
_, err = pidFile.Write(pidBytes)
if err != nil {
log.Errorf("failed to write pidfile: %v", err)
}
}
}
@ -175,7 +179,6 @@ func nodeMain() {
setupLogging(config.Log)
// only after logging setup, we may became daemon
println(pidPath)
if daemonize {
becomeDaemon()
}