Go to file
kingecg eff08899d1 chore: update module path to git.kingecg.top 2025-06-25 22:34:04 +08:00
.vscode add code 2023-12-10 18:01:54 +08:00
test chore: update module path to git.kingecg.top 2025-06-25 22:34:04 +08:00
vendor update 2024-10-02 18:38:36 +08:00
.gitignore update 2024-10-02 18:38:36 +08:00
LICENSE Initial commit 2023-12-10 17:55:49 +08:00
README.md chore: update module path to git.kingecg.top 2025-06-25 22:34:04 +08:00
go.mod chore: update module path to git.kingecg.top 2025-06-25 22:34:04 +08:00
go.sum chore: update module path to git.kingecg.top 2025-06-25 22:34:04 +08:00
godaemon.go chore: update module path to git.kingecg.top 2025-06-25 22:34:04 +08:00

README.md

godaemon

A simple daemon library for Go.

Features:

  • provide a daemon process
  • can use command to start/stop/restart the daemon

Usage:

import (
	"time"

	"module git.kingecg.top/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