proxy console log to daemon process

This commit is contained in:
程广 2024-11-17 21:31:22 +08:00
parent 9cb6ae5105
commit 05d0c60338
4 changed files with 66 additions and 8 deletions

View File

@ -1,2 +1,64 @@
# godaemon # 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
}
}
```
命令:
- <app> without arguments: start the daemon
- <app> -s quit: stop the daemon
- <app> -s restart: restart the daemon

View File

@ -164,6 +164,8 @@ func (g *GoDaemon) startTask() *exec.Cmd {
} else { } else {
cmd = exec.Command(execName) cmd = exec.Command(execName)
} }
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(cmd.Env, daemon_env_key+"="+daemon_task) cmd.Env = append(cmd.Env, daemon_env_key+"="+daemon_task)
cmd.Start() cmd.Start()
return cmd return cmd

BIN
test/main Executable file

Binary file not shown.

View File

@ -13,10 +13,7 @@ func main() {
gologger.Configure(gologger.LoggersConfig{ gologger.Configure(gologger.LoggersConfig{
Appenders: map[string]gologger.LogAppenderConfig{ Appenders: map[string]gologger.LogAppenderConfig{
"flog": { "flog": {
Type: "file", Type: "console",
Options: map[string]interface{}{
"file": "flog.log",
},
}, },
}, },
Categories: map[string]gologger.LogConfig{ Categories: map[string]gologger.LogConfig{
@ -49,8 +46,5 @@ func stop(g *godaemon.GoDaemon) {
l.Debug("task is nil") l.Debug("task is nil")
return return
} }
if g != nil && g.Running != nil {
l.Debug("To kill")
g.Running.Process.Kill()
}
} }