Documentation

Overview

    Package gproc implements management and communication for processes.

    Index

    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 Listen

          func Listen()

            ListenSignal blocks and does signal listening and handling.

            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 Pid

                func Pid() int

                  Pid returns the pid of current process.

                  func SearchBinary

                  func SearchBinary(file string) string

                    SearchBinary searches the binary <file> in current working folder and PATH environment.

                    func SearchBinaryPath

                    func SearchBinaryPath(file string) string

                      SearchBinaryPath searches the binary <file> in PATH environment.

                      func Send

                      func Send(pid int, data []byte, group ...string) error

                        Send sends data to specified process of given pid.

                        func SetPPid

                        func SetPPid(ppid int) error

                          SetPPid sets custom parent pid for current process.

                          func Shell

                          func Shell(cmd string, out io.Writer, in io.Reader) error

                            Shell executes command <cmd> synchronizingly 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

                            func ShellExec(cmd string, environment ...[]string) (string, error)

                              ShellExec executes given command <cmd> synchronizingly and returns the command result.

                              func ShellRun

                              func ShellRun(cmd string) error

                                ShellRun executes given command <cmd> synchronizingly and outputs the command result to the stdout.

                                func StartTime

                                func StartTime() time.Time

                                  StartTime returns the start time of current process.

                                  func Uptime

                                  func Uptime() time.Duration

                                    Uptime returns the duration which current process has been running

                                    Types

                                    type Manager

                                    type Manager struct {
                                    	// contains filtered or unexported fields
                                    }

                                      进程管理器

                                      func NewManager

                                      func NewManager() *Manager

                                        创建一个进程管理器

                                        func (*Manager) AddProcess

                                        func (m *Manager) AddProcess(pid int)

                                          添加一个已存在进程到进程管理器中

                                          func (*Manager) Clear

                                          func (m *Manager) Clear()

                                            清空管理器

                                            func (*Manager) GetProcess

                                            func (m *Manager) GetProcess(pid int) *Process

                                              获取当前进程管理器中的一个进程

                                              func (*Manager) KillAll

                                              func (m *Manager) KillAll() error

                                                关闭所有的进程

                                                func (*Manager) NewProcess

                                                func (m *Manager) NewProcess(path string, args []string, environment []string) *Process

                                                  创建一个进程(不执行)

                                                  func (*Manager) Pids

                                                  func (m *Manager) Pids() []int

                                                    获取所有的进程pid,构成列表返回

                                                    func (*Manager) Processes

                                                    func (m *Manager) Processes() []*Process

                                                      获取所有的进程对象,构成列表返回

                                                      func (*Manager) RemoveProcess

                                                      func (m *Manager) RemoveProcess(pid int)

                                                        移除进程管理器中的指定进程

                                                        func (*Manager) Send

                                                        func (m *Manager) Send(data []byte)

                                                          向所有进程发送消息

                                                          func (*Manager) SendTo

                                                          func (m *Manager) SendTo(pid int, data []byte) error

                                                            向指定进程发送消息

                                                            func (*Manager) SignalAll

                                                            func (m *Manager) SignalAll(sig os.Signal) error

                                                              向所有进程发送信号量

                                                              func (*Manager) Size

                                                              func (m *Manager) Size() int

                                                                当前进程总数

                                                                func (*Manager) WaitAll

                                                                func (m *Manager) WaitAll()

                                                                  等待所有子进程结束

                                                                  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

                                                                        type Process struct {
                                                                        	exec.Cmd
                                                                        	Manager *Manager
                                                                        	PPid    int
                                                                        }

                                                                          Process is the struct for a single process.

                                                                          func NewProcess

                                                                          func NewProcess(path string, args []string, environment ...[]string) *Process

                                                                            NewProcess creates and returns a new Process.

                                                                            func NewProcessCmd

                                                                            func NewProcessCmd(cmd string, environment ...[]string) *Process

                                                                              NewProcessCmd creates and returns a process with given command and optional environment variable array.

                                                                              func (*Process) Kill

                                                                              func (p *Process) Kill() error

                                                                                Kill causes the Process to exit immediately.

                                                                                func (*Process) Pid

                                                                                func (p *Process) Pid() int

                                                                                  PID

                                                                                  func (*Process) Release

                                                                                  func (p *Process) Release() error

                                                                                    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) Run

                                                                                    func (p *Process) Run() error

                                                                                      Run executes the process in blocking way.

                                                                                      func (*Process) Send

                                                                                      func (p *Process) Send(data []byte) error

                                                                                        Send send custom data to the process.

                                                                                        func (*Process) Signal

                                                                                        func (p *Process) Signal(sig os.Signal) error

                                                                                          Signal sends a signal to the Process. Sending Interrupt on Windows is not implemented.

                                                                                          func (*Process) Start

                                                                                          func (p *Process) Start() (int, error)

                                                                                            Start starts executing the process in non-blocking way. It returns the pid if success, or else it returns an error.

                                                                                            type SigHandler

                                                                                            type SigHandler func(sig os.Signal)

                                                                                              SigHandler defines a function type for signal handling.