process

package
v1.24.3 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

README

process Package

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Batch added in v1.5.2

func Batch(h host.Host, batch []Program, options ...Options) (done chan struct{}, err error)

Batch executes the slice of Program entries using Start. If any stage returns err then Batch returns immediately. Set IgnoreErr in Program to not return errors for each stage. If any stage has Restart set and it is supported then a reaper is run and the done channel returned.

func Daemon

func Daemon(writepid io.WriteCloser, processArgs func([]string, ...string) []string, args ...string) (err error)

Daemon backgrounds the current process by re-executing the existing binary (as found by os.Executable, so may there is a small window while the referenced binary can change). The function passed as processArgs is called with any further arguments passed to it as parameters and can be used to remove flags that triggered the daemonisation in the first place. A helper function - RemoveArgs - is available to do this.

If successful the function never returns and the child process PID is written to writepid, if not nil. Remember to only open the file inside the test for daemon mode in the caller, otherwise on re-execution the file will be re-opened and overwrite the one from the parent. writepid is closed in the parent.

On failure the function does return with an error.

process.Daemon(os.Stdout, process.RemoveArgs, "-D", "--daemon")

func Daemon2 added in v1.14.2

func Daemon2(writepid io.WriteCloser, addArgs []string, processArgs func([]string, ...string) []string, args ...string) (err error)

Daemon2 backgrounds the current process by re-executing the existing binary (as found by os.Executable, so may there is a small window while the referenced binary can change). The function passed as processArgs is called with any further arguments passed to it as parameters and can be used to remove flags that triggered the daemonisation in the first place. A helper function - RemoveArgs - is available to do this.

The child process is executed with any `addArgs` appended to the command line.

If successful the function never returns and the child process PID is written to writepid, if not nil. Remember to only open the file inside the test for daemon mode in the caller, otherwise on re-execution the file will be re-opened and overwrite the one from the parent. writepid is closed in the parent.

On failure the function does return with an error.

process.Daemon(os.Stdout, process.RemoveArgs, "-D", "--daemon")

func GetPID added in v1.5.2

func GetPID(h host.Host, binary string, resetcache bool, customCheckFunc func(checkarg any, cmdline []string) bool, checkarg any, args ...string) (int, error)

GetPID returns the PID of the process started with binary name and all args (in any order) on host h. If not found then an err of os.ErrProcessDone is returned.

customCheckFunc() is a custom function to validate the args against the each process found and checkargs is passed to the function as a parameter. If the function returns true then the process is a match.

walk the /proc directory (local or remote) and find the matching pid. This is subject to races, but not much we can do

We use a process cache to avoid repeated calls to the host to get the process entries, which can be expensive. The cache is updated every 5 seconds, or when the cache is empty.

func RemoveArgs

func RemoveArgs(in []string, remove ...string) (out []string)

RemoveArgs is a helper function for Daemon(). Daemon calls the function with os.Args[1:] and removes any arguments matching members of the slice `remove` and returns the result. Only bare arguments are removed and no pattern matching or adjacent values are removed. If more complex tests are required then pass Daemon() your own function.

func Start added in v1.5.2

func Start(h host.Host, program Program, options ...Options) (pid int, err error)

Start runs a process on host h. It is run detached in the background unless Foreground is true.

TODO: return error windows TODO: look at remote processes

Types

type Options added in v1.6.0

type Options func(*processOptions)

Options for Start and Batch

func ExpandArgs added in v1.6.0

func ExpandArgs() Options

ExpandArgs controls the expansion of the values in the Args slice. If this option is used then each element of the Args slice is passed to ExpandString with an lookup tables set using LookupTable().

func ExpandEnv added in v1.6.0

func ExpandEnv() Options

ExpandEnv controls the expansion of the values in the Env slice. If this option is used then each element of the Env slice is passed to ExpandString with an lookup tables set using LookupTable().

func LookupTable added in v1.6.0

func LookupTable(table map[string]string) Options

LookupTable adds a lookup map (string to string) to the set of lookup tables passed to ExpandString.

type ProcessInfo added in v1.14.2

type ProcessInfo struct {
	PID          int
	Exe          string
	Cmdline      []string
	CreationTime time.Time
	UID          int
	GID          int
	TCPPorts     []int
	UDPPorts     []int
}

ProcessInfo is a struct that holds information about a process. It is used to return information about a process on a host.

type Program added in v1.5.2

type Program struct {
	Executable string        `json:"executable,omitempty"` // Path to program, passed through exec.LookPath
	Username   string        `json:"username,omitempty"`   // The name of the user, if empty use current
	WorkingDir string        `json:"workingdir,omitempty"` // The working directory, defaults to home dir of user
	ErrLog     string        `json:"errlog,omitempty"`     // The name of the logfile, defaults to basename of program+".log" in Dir
	Args       []string      `json:"args,omitempty"`       // Args not including program
	Env        []string      `json:"env,omitempty"`        // Env as key=value pairs
	Foreground bool          `json:"foreground,omitempty"` // Run in foreground, to completion, return if result != 0
	Restart    bool          `json:"restart,omitempty"`    // restart on exit
	IgnoreErr  bool          `json:"ignoreerr,omitempty"`  // If true do not return err on failure
	Wait       time.Duration `json:"wait,omitempty"`       // period to wait after starting, default none
}

Program is a highly simplified representation of a program to manage with Start or Batch.

Args and Env can be either a comma delimited string, which is split by viper/mapstructure, or a slice of strings which is used as-is

Jump to

Keyboard shortcuts

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