Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Feb 27, 2020
1 parent fe2dd6c commit 3937bb5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion trojan/trojan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 13 additions & 20 deletions util/command.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package util

import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
"time"
)

// CheckCommandExists 检查命令是否存在
Expand Down Expand Up @@ -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
}

0 comments on commit 3937bb5

Please sign in to comment.