2,190
社区成员
发帖
与我相关
我的任务
分享
import (
"os/exec"
"context"
)
func main() {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second * 10) //10秒timeout
defer cancel()
t1 := time.Now().Unix()
out, err := exec.CommandContext(ctx, "sleep", "20").Output() //例子,sleep20秒
//out, err := exec.CommandContext(ctx, "/usr/local/bin/mysql/mysql -u root").Output()
t2 := time.Now().Unix()
if err != nil {
fmt.Printf("err=%v\n", err)
}
fmt.Printf("out=%v, time=%v\n", out, t2-t1)
}