mirror of https://github.com/dnomd343/XProxy.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.3 KiB
57 lines
1.3 KiB
package process
|
|
|
|
import (
|
|
log "github.com/sirupsen/logrus"
|
|
"os"
|
|
"os/exec"
|
|
"syscall"
|
|
)
|
|
|
|
type Process struct {
|
|
name string
|
|
env []string
|
|
command []string
|
|
process *exec.Cmd
|
|
}
|
|
|
|
func New(command ...string) *Process {
|
|
process := new(Process)
|
|
process.name = command[0]
|
|
process.command = command
|
|
log.Debugf("New process %s -> %v", process.name, process.command)
|
|
return process
|
|
}
|
|
|
|
func (p *Process) Run(isOutput bool, env []string) {
|
|
p.process = exec.Command(p.command[0], p.command[1:]...)
|
|
if isOutput {
|
|
p.process.Stdout = os.Stdout
|
|
p.process.Stderr = os.Stderr
|
|
}
|
|
p.env = env
|
|
if len(p.env) != 0 {
|
|
p.process.Env = p.env
|
|
log.Infof("Process %s with env -> %v", p.name, p.env)
|
|
}
|
|
err := p.process.Start()
|
|
if err != nil {
|
|
log.Errorf("Failed to start %s -> %v", p.name, err)
|
|
}
|
|
log.Infof("Start process %s -> PID = %d", p.name, p.process.Process.Pid)
|
|
}
|
|
|
|
func (p *Process) Signal(signal syscall.Signal) {
|
|
if p.process != nil {
|
|
log.Debugf("Send signal %v to %s", signal, p.name)
|
|
_ = p.process.Process.Signal(signal)
|
|
}
|
|
}
|
|
|
|
func (p *Process) Wait() {
|
|
if p.process != nil {
|
|
err := p.process.Wait()
|
|
if err != nil {
|
|
log.Warningf("Wait process %s -> %v", p.name, err)
|
|
}
|
|
}
|
|
}
|
|
|