优化 taskman

This commit is contained in:
dengsgo 2019-12-27 18:09:56 +08:00
parent 66eee100f2
commit 9defc968ba

View File

@ -1,9 +1,6 @@
package main package main
import ( import (
"bufio"
"fmt"
"io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@ -64,38 +61,28 @@ func (t *TaskMan) run(cf *changedFile) {
t.cmd = exec.Command(carr[0], carr[1:]...) t.cmd = exec.Command(carr[0], carr[1:]...)
//cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: syscall.CREATE_UNICODE_ENVIRONMENT} //cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: syscall.CREATE_UNICODE_ENVIRONMENT}
t.cmd.Stdin = os.Stdin t.cmd.Stdin = os.Stdin
//cmd.Stdout = os.Stdout t.cmd.Stdout = os.Stdout
t.cmd.Stderr = os.Stderr t.cmd.Stderr = os.Stderr
t.cmd.Dir = projectFolder t.cmd.Dir = projectFolder
t.cmd.Env = os.Environ() t.cmd.Env = os.Environ()
stdout, err := t.cmd.StdoutPipe() err := t.cmd.Start()
if err != nil {
log.Println(PreError, err.Error())
return
}
err = t.cmd.Start()
if err != nil { if err != nil {
log.Println(PreError, "run command", carr, "error. ", err) log.Println(PreError, "run command", carr, "error. ", err)
} break
reader := bufio.NewReader(stdout)
for {
line, err2 := reader.ReadString('\n')
if err2 != nil || io.EOF == err2 {
break
}
fmt.Print(line)
} }
err = t.cmd.Wait() err = t.cmd.Wait()
if err != nil { if err != nil {
log.Println(PreWarn, "cmd wait err ", err) log.Println(PreWarn, "command exec failed:", carr, err)
break break
} }
if t.cmd.Process != nil { if t.cmd.Process != nil {
if err = t.cmd.Process.Kill(); err != nil { err := t.cmd.Process.Kill()
log.Println(PreWarn, "cmd cannot kill, reason:", err) log.Println(t.cmd.ProcessState)
if t.cmd.ProcessState != nil && !t.cmd.ProcessState.Exited() {
log.Println(PreError, "command cannot stop!", carr, err)
} }
} }
} }
log.Println("end ") log.Println("EXEC end")
} }