From 659b6681b236ff29fc0eb29c51fb81804d3089fa Mon Sep 17 00:00:00 2001 From: Davies Liu Date: Mon, 18 Jan 2021 15:40:00 +0800 Subject: [PATCH 1/2] add a callback before exit --- daemon.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/daemon.go b/daemon.go index 06062ad..2b42650 100644 --- a/daemon.go +++ b/daemon.go @@ -30,6 +30,7 @@ type DaemonAttr struct { ProgramName string // child's os.Args[0]; copied from parent if empty CaptureOutput bool // whether to capture stdout/stderr Files []**os.File // files to keep open in the daemon + OnExit func(stage int) error } /* @@ -209,6 +210,9 @@ func MakeDaemon(attrs *DaemonAttr) (io.Reader, io.Reader, error) { return fatal(fmt.Errorf("can't create process %s: %s", procName, err)) } proc.Release() + if attrs.OnExit != nil { + attrs.OnExit(stage) + } os.Exit(0) } From 3da5144a127d2baef7fdf01c9523d4aa3b33cf0c Mon Sep 17 00:00:00 2001 From: Davies Liu Date: Tue, 29 Jun 2021 12:55:18 +0800 Subject: [PATCH 2/2] add a option to redirect stdout/stderr to a file --- daemon.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/daemon.go b/daemon.go index 2b42650..05412b1 100644 --- a/daemon.go +++ b/daemon.go @@ -30,6 +30,7 @@ type DaemonAttr struct { ProgramName string // child's os.Args[0]; copied from parent if empty CaptureOutput bool // whether to capture stdout/stderr Files []**os.File // files to keep open in the daemon + Stdout *os.File // redirect stdout/stderr to it OnExit func(stage int) error } @@ -143,7 +144,12 @@ func MakeDaemon(attrs *DaemonAttr) (io.Reader, io.Reader, error) { if err != nil { return fatal(err) } - files[0], files[1], files[2] = nullDev, nullDev, nullDev + files[0] = nullDev + if attrs.Stdout != nil { + files[1], files[2] = attrs.Stdout, attrs.Stdout + } else { + files[1], files[2] = nullDev, nullDev + } fd := 3 for _, fPtr := range attrs.Files {