diff --git a/README.md b/README.md index 4feb83b..6022c04 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,64 @@ # godaemon +A simple daemon library for Go. + +Features: +- provide a daemon process +- can use command to start/stop/restart the daemon + +Usage: +```go +import ( + "time" + + "git.pyer.club/kingecg/godaemon" + "git.pyer.club/kingecg/gologger" +) + +var daemon *godaemon.GoDaemon + +func main() { + gologger.Configure(gologger.LoggersConfig{ + Appenders: map[string]gologger.LogAppenderConfig{ + "flog": { + Type: "console", + }, + }, + Categories: map[string]gologger.LogConfig{ + "default": { + Appenders: []string{"flog"}, + Level: "debug", + }, + }, + }) + daemon := godaemon.NewGoDaemon(start, stop) + daemon.Start() +} + +func start(g *godaemon.GoDaemon) { + l := gologger.GetLogger("task") + for { + time.Sleep(time.Second * 1) + l.Debug("task running:", g.GetPid()) + } +} + +func stop(g *godaemon.GoDaemon) { + l := gologger.GetLogger("task") + l.Debug("called stop") + if g == nil { + l.Debug("Daemon is nil") + return + } + if g.Running == nil { + l.Debug("task is nil") + return + } + +} +``` + +命令: +- without arguments: start the daemon +- -s quit: stop the daemon +- -s restart: restart the daemon \ No newline at end of file diff --git a/godaemon.go b/godaemon.go index 08b8cc3..06d467c 100644 --- a/godaemon.go +++ b/godaemon.go @@ -164,6 +164,8 @@ func (g *GoDaemon) startTask() *exec.Cmd { } else { cmd = exec.Command(execName) } + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr cmd.Env = append(cmd.Env, daemon_env_key+"="+daemon_task) cmd.Start() return cmd diff --git a/test/main b/test/main new file mode 100755 index 0000000..6d1e6b5 Binary files /dev/null and b/test/main differ diff --git a/test/main.go b/test/main.go index 2f3dd95..27f2bd5 100644 --- a/test/main.go +++ b/test/main.go @@ -13,10 +13,7 @@ func main() { gologger.Configure(gologger.LoggersConfig{ Appenders: map[string]gologger.LogAppenderConfig{ "flog": { - Type: "file", - Options: map[string]interface{}{ - "file": "flog.log", - }, + Type: "console", }, }, Categories: map[string]gologger.LogConfig{ @@ -49,8 +46,5 @@ func stop(g *godaemon.GoDaemon) { l.Debug("task is nil") return } - if g != nil && g.Running != nil { - l.Debug("To kill") - g.Running.Process.Kill() - } + }