execd

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2020 License: Apache-2.0 Imports: 9 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotRunning ...
	ErrNotRunning = errors.New("execd: process is not running")
)

Functions

This section is empty.

Types

type D

type D struct {
	// Path is the path of the command to run.
	//
	// This is the only field that must be set to a non-zero
	// value. If Path is relative, it is evaluated relative
	// to Dir.
	Path string

	// Args holds command line arguments, including the command as Args[0].
	// If the Args field is empty or nil, Run uses {Path}.
	//
	// In typical use, both Path and Args are set by calling Command.
	Args []string

	// Env specifies the environment of the process.
	// Each entry is of the form "key=value".
	// If Env is nil, the new process uses the current process's
	// environment.
	// If Env contains duplicate environment keys, only the last
	// value in the slice for each duplicate key is used.
	Env []string

	// Dir specifies the working directory of the command.
	// If Dir is the empty string, Run runs the command in the
	// calling process's current directory.
	Dir string

	// Stdin specifies the process's standard input.
	//
	// If Stdin is nil, the process reads from the null device (os.DevNull).
	//
	// If Stdin is an *os.File, the process's standard input is connected
	// directly to that file.
	//
	// Otherwise, during the execution of the command a separate
	// goroutine reads from Stdin and delivers that data to the command
	// over a pipe. In this case, Wait does not complete until the goroutine
	// stops copying, either because it has reached the end of Stdin
	// (EOF or a read error) or because writing to the pipe returned an error.
	Stdin io.Reader

	// Stdout and Stderr specify the process's standard output and error.
	//
	// If either is nil, Run connects the corresponding file descriptor
	// to the null device (os.DevNull).
	//
	// If either is an *os.File, the corresponding output from the process
	// is connected directly to that file.
	//
	// Otherwise, during the execution of the command a separate goroutine
	// reads from the process over a pipe and delivers that data to the
	// corresponding Writer. In this case, Wait does not complete until the
	// goroutine reaches EOF or encounters an error.
	//
	// If Stdout and Stderr are the same writer, and have a type that can
	// be compared with ==, at most one goroutine at a time will call Write.
	Stdout io.Writer
	Stderr io.Writer

	// SysProcAttr holds optional, operating system-specific attributes.
	// Run passes it to os.StartProcess as the os.ProcAttr's Sys field.
	SysProcAttr *syscall.SysProcAttr
	// contains filtered or unexported fields
}

D represents an external daemon command being prepared or run.

A daemon is a long-time running background process providing reliable services for others. If your program needs graceful shutdown, you can SetGracePeriod or SetGracefulShutDown.

func Daemon

func Daemon(name string, arg ...string) *D

Daemon returns the D struct to execute the named program with the given arguments.

It sets only the Path and Args in the returned structure.

If name contains no path separators, Command uses exec.LookPath to resolve name to a complete path if possible. Otherwise it uses name directly as Path.

The returned D's Args field is constructed from the command name followed by the elements of arg, so arg should not include the command name itself. For example, Daemon("echo", "hello"). Args[0] is always name, not the possibly resolved Path.

func DaemonFrom

func DaemonFrom(c *exec.Cmd) *D

DaemonFrom retrieves the necessary information from given exec.Cmd and returns a new D struct

func (*D) Command

func (c *D) Command() *exec.Cmd

Command returns the running exec.Cmd struct in D

func (*D) IsRunning

func (c *D) IsRunning() bool

IsRunning returns true if the daemon is still running background

func (*D) Name

func (c *D) Name() string

Name returns the name of daemon Generally, it is the first arg in Args

func (*D) Pid

func (c *D) Pid() (int, error)

Pid returns the running process's pid if the daemon is not running, it will return ErrNotRunning

func (*D) RunForever

func (c *D) RunForever() error

RunForever starts the specified command and waits for it to complete in another goroutine. If there is no error, the daemon will run forever.

In the meantime, It starts a goroutine to keep the background process alive. But if the error occurs more than `crashBackOff` times when command is starting, it will stop tracking anymore.

func (*D) SetGracePeriod

func (c *D) SetGracePeriod(d time.Duration)

SetGracePeriod sets graceful shutdown period time It will be invalidated when SetGracefulShutDown is called

If the grace period == 0, execd sends SIGKILL to the daemon process immediately If the grace period > 0 , execd sends SIGTERM to the daemon. If the daemon doesn't terminate within the grace period, a SIGKILL will be sent and the daemon violently terminated

func (*D) SetGracefulShutDown

func (c *D) SetGracefulShutDown(f func(*exec.Cmd) error)

SetGracefulShutDown sets graceful shutdown handler to override the default behavior

func (*D) Signal

func (c *D) Signal(signal os.Signal) error

Signal sends a signal to the daemon pocess. if the daemon is not running, it will return an ErrNotRunning

func (*D) Stop

func (c *D) Stop() error

Stop stops the daemon process

Jump to

Keyboard shortcuts

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