Browse Source

feat: demo of Process class

v1.x.x
dnomd343 2 years ago
parent
commit
fc70d5b782
  1. 12
      src/main.go
  2. 53
      src/process.go

12
src/main.go

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -23,6 +24,13 @@ func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
log.Warning("XProxy start") log.Warning("XProxy start")
ls := newProcess("ls", "-al")
ls.startProcess(true, true)
fmt.Println(ls.isProcessAlive())
ls.waitProcess()
fmt.Println(ls.isProcessAlive())
//content, err := os.ReadFile("test.yml") //content, err := os.ReadFile("test.yml")
//if err != nil { //if err != nil {
// panic(err) // panic(err)
@ -30,8 +38,8 @@ func main() {
//loadConfig(content) //loadConfig(content)
//loadProxy("/etc/xproxy/config", "/xproxy") //loadProxy("/etc/xproxy/config", "/xproxy")
loadGeoIp("/xproxy/assets") //loadGeoIp("/xproxy/assets")
loadGeoSite("/xproxy/assets") //loadGeoSite("/xproxy/assets")
// TODO: auto-update assets file (by cron command) // TODO: auto-update assets file (by cron command)
//loadDns() //loadDns()

53
src/process.go

@ -0,0 +1,53 @@
package main
import (
log "github.com/sirupsen/logrus"
"os"
"os/exec"
"syscall"
)
type Process struct {
command []string
process *exec.Cmd
}
func newProcess(command ...string) *Process {
process := new(Process)
process.command = command
log.Debugf("New process -> %v", command)
return process
}
func (p *Process) startProcess(isStdout bool, isStderr bool) {
p.process = exec.Command(p.command[0], p.command[1:]...)
if isStdout {
p.process.Stdout = os.Stdout
}
if isStderr {
p.process.Stderr = os.Stderr
}
err := p.process.Start()
if err != nil {
log.Errorf("Failed to start %v -> %v", p.command, err)
}
log.Infof("Start process %v -> PID = %d", p.command, p.process.Process.Pid)
}
func (p *Process) isProcessAlive() bool {
return p.process.ProcessState == nil
}
func (p *Process) sendSignal(signal syscall.Signal) {
err := p.process.Process.Signal(signal)
if err != nil {
log.Errorf("Send signal %v error -> %v", signal, p.command)
}
}
func (p *Process) waitProcess() {
err := p.process.Wait()
if err != nil {
log.Errorf("Wait process error -> %v", p.command)
}
}
Loading…
Cancel
Save