diff --git a/trojan/trojan.go b/trojan/trojan.go index 8828988c..b72b8ddf 100644 --- a/trojan/trojan.go +++ b/trojan/trojan.go @@ -41,7 +41,7 @@ func Start() { Stop() } util.ExecCommand(`echo "" > /.run.log`) - util.ExecCommandWithTimeout(`/usr/bin/trojan/trojan -c /usr/local/etc/trojan/config.json -l /.run.log & -y`) + util.StartProcess("/usr/bin/trojan/trojan", "-c", "/usr/local/etc/trojan/config.json", "-l", "/.run.log") fmt.Println(util.Green("启动trojan成功!")) } else { if err := util.ExecCommand("systemctl start trojan"); err != nil { diff --git a/util/command.go b/util/command.go index 7c88304a..ac7211f1 100644 --- a/util/command.go +++ b/util/command.go @@ -1,15 +1,13 @@ package util import ( - "bytes" - "context" "fmt" "io" "io/ioutil" "net/http" + "os" "os/exec" "strings" - "time" ) // CheckCommandExists 检查命令是否存在 @@ -89,23 +87,18 @@ func ExecCommandWithResult(command string) string { return string(out) } -func ExecCommandWithTimeout(command string) ([]byte, error) { - ctxt, cancel := context.WithTimeout(context.Background(), 3*time.Second) - defer cancel() - - cmd := exec.CommandContext(ctxt, "bash", "-c", command) - - var buf bytes.Buffer - cmd.Stdout = &buf - cmd.Stderr = &buf - - if err := cmd.Start(); err != nil { - return buf.Bytes(), err +func StartProcess(name string, args ...string) { + env := os.Environ() + procAttr := &os.ProcAttr{ + Env: env, + Files: []*os.File{ + os.Stdin, + os.Stdout, + os.Stderr, + }, } - - if err := cmd.Wait(); err != nil { - return buf.Bytes(), err + _, err := os.StartProcess(name, args, procAttr) + if err != nil { + fmt.Println(err) } - - return buf.Bytes(), nil }