Documentation ¶
Overview ¶
Package gproc implements management and communication for processes.
Index ¶
- func AddSigHandler(handler SigHandler, signals ...os.Signal)
- func AddSigHandlerShutdown(handler ...SigHandler)
- func IsChild() bool
- func Listen()
- func PPid() int
- func PPidOS() int
- func Pid() int
- func SearchBinary(file string) string
- func SearchBinaryPath(file string) string
- func Send(pid int, data []byte, group ...string) error
- func SetPPid(ppid int) error
- func Shell(cmd string, out io.Writer, in io.Reader) error
- func ShellExec(cmd string, environment ...[]string) (string, error)
- func ShellRun(cmd string) error
- func StartTime() time.Time
- func Uptime() time.Duration
- type Manager
- func (m *Manager) AddProcess(pid int)
- func (m *Manager) Clear()
- func (m *Manager) GetProcess(pid int) *Process
- func (m *Manager) KillAll() error
- func (m *Manager) NewProcess(path string, args []string, environment []string) *Process
- func (m *Manager) Pids() []int
- func (m *Manager) Processes() []*Process
- func (m *Manager) RemoveProcess(pid int)
- func (m *Manager) Send(data []byte)
- func (m *Manager) SendTo(pid int, data []byte) error
- func (m *Manager) SignalAll(sig os.Signal) error
- func (m *Manager) Size() int
- func (m *Manager) WaitAll()
- type MsgRequest
- type MsgResponse
- type Process
- type SigHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddSigHandler ¶
func AddSigHandler(handler SigHandler, signals ...os.Signal)
AddSigHandler adds custom signal handler for custom one or more signals.
func AddSigHandlerShutdown ¶
func AddSigHandlerShutdown(handler ...SigHandler)
AddSigHandlerShutdown adds custom signal handler for shutdown signals: syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGABRT.
func IsChild ¶
func IsChild() bool
IsChild checks and returns whether current process is a child process. A child process is forked by another gproc process.
func PPid ¶
func PPid() int
PPid returns the custom parent pid if exists, or else it returns the system parent pid.
func PPidOS ¶
func PPidOS() int
PPidOS returns the system parent pid of current process. Note that the difference between PPidOS and PPid function is that the PPidOS returns the system ppid, but the PPid functions may return the custom pid by gproc if the custom ppid exists.
func SearchBinary ¶
SearchBinary searches the binary `file` in current working folder and PATH environment.
func SearchBinaryPath ¶
SearchBinaryPath searches the binary `file` in PATH environment.
func Send ¶
Send sends data to specified process of given pid.
func Shell ¶
Shell executes command `cmd` synchronously with given input pipe `in` and output pipe `out`. The command `cmd` reads the input parameters from input pipe `in`, and writes its output automatically to output pipe `out`.
func ShellExec ¶
ShellExec executes given command `cmd` synchronously and returns the command result.
func ShellRun ¶
ShellRun executes given command `cmd` synchronously and outputs the command result to the stdout.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager is a process manager maintaining multiple processes.
func (*Manager) AddProcess ¶
AddProcess adds a process to current manager. It does nothing if the process with given `pid` does not exist.
func (*Manager) GetProcess ¶
GetProcess retrieves and returns a Process object. It returns nil if it does not find the process with given `pid`.
func (*Manager) KillAll ¶
KillAll kills all processes in current manager.
func (*Manager) NewProcess ¶
NewProcess creates and returns a Process object.
func (*Manager) Pids ¶
Pids retrieves and returns all process id array in current manager.
func (*Manager) Processes ¶
Processes retrieves and returns all processes in current manager.
func (*Manager) RemoveProcess ¶
RemoveProcess removes a process from current manager.
func (*Manager) Send ¶
Send sends data bytes to all processes in current manager.
func (*Manager) SendTo ¶
SendTo sneds data bytes to specified processe in current manager.
func (*Manager) SignalAll ¶
SignalAll sends a signal `sig` to all processes in current manager.
func (*Manager) Size ¶
Size returns the size of processes in current manager.
type MsgRequest ¶
type MsgRequest struct { SendPid int // Sender PID. RecvPid int // Receiver PID. Group string // Message group name. Data []byte // Request data. }
MsgRequest is the request structure for process communication.
func Receive ¶
func Receive(group ...string) *MsgRequest
Receive blocks and receives message from other process using local TCP listening. Note that, it only enables the TCP listening service when this function called.
type MsgResponse ¶
type MsgResponse struct { Code int // 1: OK; Other: Error. Message string // Response message. Data []byte // Response data. }
MsgResponse is the response structure for process communication.
type Process ¶
Process is the struct for a single process.
func NewProcess ¶
NewProcess creates and returns a new Process.
func NewProcessCmd ¶
NewProcessCmd creates and returns a process with given command and optional environment variable array.
func (*Process) Pid ¶
Pid retrieves and returns the PID for the process.
func (*Process) Release ¶
Release releases any resources associated with the Process p, rendering it unusable in the future. Release only needs to be called if Wait is not.
func (*Process) Send ¶
Send sends custom data to the process.
func (*Process) Signal ¶
Signal sends a signal to the Process. Sending Interrupt on Windows is not implemented.