diff --git a/.github/ISSUE_TEMPLATE/default-issue-template.md b/.github/ISSUE_TEMPLATE/default-issue-template.md index 0ee9f7f..7b7a4bb 100644 --- a/.github/ISSUE_TEMPLATE/default-issue-template.md +++ b/.github/ISSUE_TEMPLATE/default-issue-template.md @@ -9,8 +9,12 @@ assignees: '' ### 描述问题 Describe the problem @@ -18,6 +22,9 @@ Make sure your dashboard and agent is up to date. ### 期待的结果 Expected result diff --git a/cmd/agent/main.go b/cmd/agent/main.go index dd3a975..ed2f89c 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -21,6 +21,7 @@ import ( "google.golang.org/grpc" "github.com/naiba/nezha/model" + "github.com/naiba/nezha/pkg/utils" pb "github.com/naiba/nezha/proto" "github.com/naiba/nezha/service/dao" "github.com/naiba/nezha/service/monitor" @@ -230,7 +231,12 @@ func doTask(task *pb.Task) { } case model.TaskTypeCommand: startedAt := time.Now() - cmd := exec.Command(task.GetData()) + var cmd *exec.Cmd + if utils.IsWindows() { + cmd = exec.Command("cmd", "/c", task.GetData()) + } else { + cmd = exec.Command("sh", "-c", task.GetData()) + } output, err := cmd.Output() result.Delay = float32(time.Now().Sub(startedAt).Seconds()) if err != nil { diff --git a/cmd/playground/example.sh b/cmd/playground/example.sh index a6a5e09..fac9411 100755 --- a/cmd/playground/example.sh +++ b/cmd/playground/example.sh @@ -3,7 +3,8 @@ set -x ME=`whoami` -ping example.com -c20 && \ +ping example.com -c3 && \ echo "==== $ME ====" && \ - ping example.net -c20 && \ + ping example.net -c3 && \ + echo $1 && \ echo "==== done! ====" \ No newline at end of file diff --git a/cmd/playground/main.go b/cmd/playground/main.go index 93402dc..73b1429 100644 --- a/cmd/playground/main.go +++ b/cmd/playground/main.go @@ -11,6 +11,7 @@ import ( "time" "github.com/go-ping/ping" + "github.com/naiba/nezha/pkg/utils" "github.com/shirou/gopsutil/v3/disk" ) @@ -77,7 +78,13 @@ func cmdExec() { if err != nil { panic(err) } - cmd := exec.Command(execFrom + "/cmd/playground/example.sh") + var cmd *exec.Cmd + if utils.IsWindows() { + cmd = exec.Command("cmd", "/c", execFrom+"/cmd/playground/example.sh hello asd") + } else { + cmd = exec.Command("sh", "-c", execFrom+`/cmd/playground/example.sh hello && \ +echo world!`) + } output, err := cmd.Output() log.Println("output:", string(output)) log.Println("err:", err) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 735eed6..69c8775 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -4,6 +4,7 @@ import ( "crypto/md5" "encoding/hex" "math/rand" + "os" "time" "unsafe" ) @@ -40,3 +41,7 @@ func MD5(plantext string) string { hash.Write([]byte(plantext)) return hex.EncodeToString(hash.Sum(nil)) } + +func IsWindows() bool { + return os.PathSeparator == '\\' && os.PathListSeparator == ';' +}