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.
 
 

51 lines
1.2 KiB

package process
import (
log "github.com/sirupsen/logrus"
"os"
"os/exec"
"syscall"
)
type Process struct {
name 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) {
p.process = exec.Command(p.command[0], p.command[1:]...)
if isOutput {
p.process.Stdout = os.Stdout
p.process.Stderr = os.Stderr
}
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)
}
}
}