daemon

package
v1.6.18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DaemonServerCommand defines a group of sub-commands for daemon operations.
	DaemonServerCommand = &cmdr.Command{
		BaseOpt: cmdr.BaseOpt{

			Short:       "s",
			Full:        "server",
			Aliases:     []string{"svr", "daemon"},
			Description: "server ops: for linux daemon.",
			Group:       "Daemonization",
		},
		Flags: []*cmdr.Flag{
			{
				BaseOpt: cmdr.BaseOpt{
					Short:       "p",
					Full:        "port",
					Description: "main port (RESTful).",
				},
				DefaultValue: 3000,
			},
		},
		SubCommands: []*cmdr.Command{
			{
				BaseOpt: cmdr.BaseOpt{
					Short:       "s",
					Full:        "start",
					Aliases:     []string{"run", "startup"},
					Description: "startup this system service/daemon.",
					Action:      daemonStart,
					LongDescription: `**start** command make program running as a daemon background.
**run** command make program running in current tty foreground.
`,
					Examples: `
$ {{.AppName}} start
					make program running as a daemon background.
$ {{.AppName}} start --foreground
					make program running in current tty foreground.
$ {{.AppName}} run
					make program running in current tty foreground.
$ {{.AppName}} stop
					stop daemonized program.
$ {{.AppName}} reload
					send signal to trigger program reload its configurations.
$ {{.AppName}} status
					display the daemonized program running status.
$ {{.AppName}} install [--systemd]
					install program as a systemd service.
$ {{.AppName}} uninstall
					remove the installed systemd service.
`,
				},
				Flags: []*cmdr.Flag{
					{
						BaseOpt: cmdr.BaseOpt{
							Short:       "f",
							Full:        "foreground",
							Aliases:     []string{"fg"},
							Description: "run on foreground, NOT daemonized.",
						},
						DefaultValue: false,
					},
				},
			},
			{
				BaseOpt: cmdr.BaseOpt{
					Short:       "t",
					Full:        "stop",
					Aliases:     []string{"stp", "halt", "pause"},
					Description: "stop this system service/daemon.",
					Action:      daemonStop,
				},
				Flags: []*cmdr.Flag{
					{
						BaseOpt: cmdr.BaseOpt{
							Short:       "1",
							Full:        "hup",
							Description: "send SIGHUP - to reload service",
						},
						DefaultValue: false,
					},
					{
						BaseOpt: cmdr.BaseOpt{
							Short:       "3",
							Full:        "quit",
							Description: "send SIGQUIT - to quit service gracefully",
						},
						DefaultValue: false,
					},
					{
						BaseOpt: cmdr.BaseOpt{
							Short:       "9",
							Full:        "kill",
							Description: "send SIGKILL - to quit service unconditionally",
						},
						DefaultValue: false,
					},
					{
						BaseOpt: cmdr.BaseOpt{
							Short:       "15",
							Full:        "term",
							Description: "send SIGTERM - to quit service gracefully",
						},
						DefaultValue: false,
					},
				},
			},
			{
				BaseOpt: cmdr.BaseOpt{
					Short:       "re",
					Full:        "restart",
					Aliases:     []string{"reload"},
					Description: "restart this system service/daemon.",
					Action:      daemonRestart,
				},
			},
			{
				BaseOpt: cmdr.BaseOpt{
					Short:       "ss",
					Full:        "status",
					Aliases:     []string{"st"},
					Description: "display its running status as a system service/daemon.",
					Action:      daemonStatus,
				},
			},
			{
				BaseOpt: cmdr.BaseOpt{
					Short:       "i",
					Full:        "install",
					Aliases:     []string{"setup"},
					Description: "install as a system service/daemon.",
					Group:       "Config",
					Action:      daemonInstall,
				},
				Flags: []*cmdr.Flag{
					{
						BaseOpt: cmdr.BaseOpt{
							Short:       "s",
							Full:        "systemd",
							Aliases:     []string{"sys"},
							Description: "install as a systemd service.",
						},
						DefaultValue: true,
					},
				},
			},
			{
				BaseOpt: cmdr.BaseOpt{
					Short:       "u",
					Full:        "uninstall",
					Aliases:     []string{"remove"},
					Description: "remove from a system service/daemon.",
					Group:       "Config",
					Action:      daemonUninstall,
				},
			},
		},
	}
)
View Source
var ErrNoRoot = errors.New("MUST have administrator privileges")

ErrNoRoot error object: `MUST have administrator privileges`

Functions

func IsRunningInDemonizedMode added in v1.1.5

func IsRunningInDemonizedMode() bool

IsRunningInDemonizedMode returns true if you are running under demonized mode. false means that you're running in normal console/tty mode.

func QuitSignals added in v1.0.0

func QuitSignals() chan os.Signal

QuitSignals return a channel for quit signal raising up.

func SetReloadSignals added in v1.1.7

func SetReloadSignals(sig func() []os.Signal)

SetReloadSignals allows an functor to provide a list of Signals

func SetSigEmtSignals added in v1.5.0

func SetSigEmtSignals(sig func() []os.Signal)

SetSigEmtSignals allows an functor to provide a list of Signals

func SetTermSignals added in v1.1.7

func SetTermSignals(sig func() []os.Signal)

SetTermSignals allows an functor to provide a list of Signals

func WithDaemon added in v1.5.0

func WithDaemon(daemonImplX Daemon,
	modifier func(daemonServerCommand *cmdr.Command) *cmdr.Command,
	preAction func(cmd *cmdr.Command, args []string) (err error),
	postAction func(cmd *cmdr.Command, args []string),
) cmdr.ExecOption

WithDaemon enables daemon plugin: - add daemon commands and sub-commands: start/run, stop, restart/reload, status, install/uninstall - pidfile -

Types

type Daemon

type Daemon interface {
	OnRun(cmd *cmdr.Command, args []string, stopCh, doneCh chan struct{}) (err error)
	OnStop(cmd *cmdr.Command, args []string) (err error)
	OnReload()
	OnStatus(cxt *impl.Context, cmd *cmdr.Command, p *os.Process) (err error)
	OnInstall(cxt *impl.Context, cmd *cmdr.Command, args []string) (err error)
	OnUninstall(cxt *impl.Context, cmd *cmdr.Command, args []string) (err error)
}

Daemon interface should be implemented when you are using `daemon.Enable()`.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL