草庐IT

CommandContext

全部标签

go - 将 exec.CommandContext 与 sg_inq sys 命令和超时一起使用永远不会返回

我正在尝试在多路径设备(/dev/mapper/mpatha)上调用sg_inq。sg_inq向特定设备发送iScsi查询并提取一些数据。我正在使用go自己的exec.commandContext超时,如下所示:ctx,cancel:=context.WithTimeout(context.Background(),time.Duration(1000)*time.Millisecond)defercancel()cmd:=exec.CommandContext(ctx,"sg_inq","/dev/mapper/mpatha")err:=cmd.Run()cmd.Stdout=&st

go - 在goroutine中使用exec.CommandContext时如何调用cancel()

我想按需取消正在运行的命令,为此,我正在尝试exec.CommandContext,当前正在尝试以下操作:https://play.golang.org/p/0JTD9HKvyadpackagemainimport("context""log""os/exec""time")funcRun(quitchanstruct{}){ctx,cancel:=context.WithCancel(context.Background())cmd:=exec.CommandContext(ctx,"sleep","300")err:=cmd.Start()iferr!=nil{log.Fatal(