ExecCommand 执行cmd命令操作
(name string, args []string, timeout ...time.Duration)
| 1078 | |
| 1079 | // ExecCommand 执行cmd命令操作 |
| 1080 | func ExecCommand(name string, args []string, timeout ...time.Duration) (out string, err error) { |
| 1081 | var ( |
| 1082 | stderr, stdout bytes.Buffer |
| 1083 | expire = 30 * time.Minute |
| 1084 | ) |
| 1085 | |
| 1086 | if len(timeout) > 0 { |
| 1087 | expire = timeout[0] |
| 1088 | } |
| 1089 | |
| 1090 | cmd := exec.Command(name, args...) |
| 1091 | cmd.Stdout = &stdout |
| 1092 | cmd.Stderr = &stderr |
| 1093 | time.AfterFunc(expire, func() { |
| 1094 | if cmd.Process != nil && cmd.Process.Pid != 0 { |
| 1095 | out = out + fmt.Sprintf("\nexecute timeout: %v seconds.", expire.Seconds()) |
| 1096 | cmd.Process.Kill() |
| 1097 | } |
| 1098 | }) |
| 1099 | err = cmd.Run() |
| 1100 | if err != nil { |
| 1101 | err = fmt.Errorf("%v\n%v", err.Error(), stderr.String()) |
| 1102 | } |
| 1103 | out = stdout.String() |
| 1104 | return |
| 1105 | } |
| 1106 | |
| 1107 | // SegWords 分词 |
| 1108 | func SegWords(sentence string, length ...int) (words []string) { |
no test coverage detected