Documentation
¶
Index ¶
- func Batch(h host.Host, batch []Program) (done chan struct{}, err error)
- func Daemon(writepid io.Writer, processArgs func([]string, ...string) []string, ...) (err error)
- func GetPID(h host.Host, binary string, args ...string) (pid int, err error)
- func RemoveArgs(in []string, remove ...string) (out []string)
- func Start(h host.Host, program Program) (pid int, err error)
- type Program
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Batch ¶ added in v1.5.2
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.Writer, 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, which can be io.Discard if not required. On failure the function does return with an error.
process.Daemon(os.Stdout, process.RemoveArgs, "-D", "--daemon")
func GetPID ¶ added in v1.5.2
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.
walk the /proc directory (local or remote) and find the matching pid. This is subject to races, but not much we can do
TODO: add support for windows hosts - the lookups are based on the host h and not the local system
func RemoveArgs ¶
RemoveArgs is a helper function for Daemon(). Daemon calls the function with os.Args[1;] as in and removes any arguments matching members of the slice remove and returns out. Only bare arguments are removed and no pattern matching or adjacent values are removed. If this is required then pass your own function with the same signature.
func Start ¶ added in v1.5.2
Start runs a process on host h. It is run detached in the background unless Foreground is true. username should be am empty string (for now). home is the working directory for the process, defaults to the home dir of the user if empty. out is the log to write stdout and stderr to. args are process args, env is a slice of key=value env vars.
TODO: return error windows TODO: look at remote processes
Types ¶
type Program ¶ added in v1.5.2
type Program struct {
Executable string // Path to program, passed through exec.LookPath
Username string // The name of the user, if empty use current
WoringDir string // The working directory, defaults to home dir of user
ErrLog string // The name of the logfile, defaults to basename of program+".log" in Dir
Args any // Args not including program
Env any // Env as key=value pairs
Foreground bool // Run in foreground, to completion, return if result != 0
Restart bool // restart on exit
IgnoreErr bool // If true do not return err on failure
Wait time.Duration // 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 string, which is split on whitespace or a slice of strings which is used as-is