Documentation ¶
Overview ¶
Package godaemon runs a program as a Unix daemon.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Daemonize ¶
func Daemonize(child ...bool)
Daemonize is equivalent to MakeDaemon(&DaemonAttr{}). It is kept only for backwards API compatibility, but it's usage is otherwise discouraged. Use MakeDaemon() instead. The child parameter, previously used to tell whether to reset the environment or not (see MakeDaemon()), is currently ignored. The environment is reset in all cases.
func GetExecutablePath ¶
GetExecutablePath returns the absolute path to the currently running executable. It is used internally by the godaemon package, and exported publicly because it's useful outside of the package too.
func MakeDaemon ¶
MakeDaemon turns the process into a daemon. But given the lack of Go's support for fork(), MakeDaemon() is forced to run the process all over again, from the start. Hence, this should probably be your first call after main begins, unless you understand the effects of calling from somewhere else. Keep in mind that the PID changes after this function is called, given that it only returns in the child; the parent will exit without returning.
Options are provided as a DaemonAttr structure. In particular, setting the CaptureOutput member to true will make the function return two io.Reader streams to read the process' standard output and standard error, respectively. That's useful if you want to capture things you'd normally lose given the lack of console output for a daemon. Some libraries can write error conditions to standard error or make use of Go's log package, that defaults to standard error too. Having these streams allows you to capture them as required. (Note that this function takes no action whatsoever on any of the streams.)
NOTE: If you use them, make sure NOT to take one of these readers and write the data back again to standard output/error, or you'll end up with a loop.
Daemonizing is a 3-stage process. In stage 0, the program increments the magical environment variable and starts a copy of itself that's a session leader, with its STDIN, STDOUT, and STDERR disconnected from any tty. It then exits.
In stage 1, the (new copy of) the program starts another copy that's not a session leader, and then exits.
In stage 2, the (new copy of) the program chdir's to /, then sets the umask and reestablishes the original value for the environment variable.
Types ¶
type DaemonAttr ¶
type DaemonAttr struct {
CaptureOutput bool // whether to capture stdout/stderr
}
DaemonAttr describes the options that apply to daemonization