sevlyar / go-daemon

A library for writing system daemons in golang.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not demonized with CLI

d-tsuji opened this issue · comments

If I use this repository with a CLI app, won't it be daemonized?

I'm running the following sample program, but unfortunately it was not daemonized.

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/sevlyar/go-daemon"
	"github.com/urfave/cli/v2"
)

func main() {
	app := cli.NewApp()
	app.Commands = []*cli.Command{
		commandAdd,
	}
	err := app.Run(os.Args)
	if err != nil {
		panic(err)
	}
}

var commandAdd = &cli.Command{
	Name:    "add",
	Aliases: []string{"add"},
	Action: func(c *cli.Context) error {
		dcnx := &daemon.Context{
			PidFileName: "sample.pid",
			PidFilePerm: 0644,
			LogFileName: "sample.log",
			LogFilePerm: 0640,
			WorkDir:     "./",
			Umask:       027,
			Args:        []string{"[go-daemon sample]"},
		}

		_, err := dcnx.Reborn()
		if err != nil {
			fmt.Errorf("unable to run: %w", err)
		}

		defer dcnx.Release()

		for i := 1; i < 10; i++ {
			time.Sleep(100 * time.Millisecond)
			fmt.Println(i)
		}
		return nil
	},
}
  • method of implementation
$ go run main.go add

Thank you.

I understand that it cannot be incorporated as a go module because it essentially requires os.Args[0].