示例#1
0
func ExampleCommandContext() {
	ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
	defer cancel()

	if err := exec.CommandContext(ctx, "sleep", "5").Run(); err != nil {
		// This will fail after 100 milliseconds. The 5 second sleep
		// will be interrupted.
	}
}
示例#2
0
文件: main.go 项目: CodyGuo/Go-Cody
func ping(ctx context.Context, ip string) {
	cmd := exec.CommandContext(ctx, "ping", ip, "-t")
	cmd.Stdout = os.Stdin

	go cmd.Run()
	<-ctx.Done()
	fmt.Println("接收到退出消息", cmd.Process.Pid)
	cmd.Process.Kill()
}
示例#3
0
文件: hilog.go 项目: suapapa/tools
func main() {
	opt, args := parseFlags()

	if opt.listBundledLSPs {
		// Print out bundled LSPs
		for b := range bundles {
			fmt.Println(b)
		}
		os.Exit(0)
	}

	var err error
	lsp, err = loadLogSchemePack(args[0])
	exitIf(err)

	if opt.dumpLSP {
		// Print out lsp in JSON format
		lsp.MarshalToJson(os.Stdout)
		os.Exit(0)
	}

	ctx, cancle := context.WithCancel(context.Background())
	defer cancle()

	args = args[1:]
	if len(args) > 0 {
		// command given as args
		cmd := exec.CommandContext(ctx, args[0], args[1:]...)
		cStdOut, err := cmd.StdoutPipe()
		exitIf(err)
		cStdErr, err := cmd.StderrPipe()
		exitIf(err)

		cmd.Start()

		go paintLines(ctx, cStdOut, "stdout")
		go paintLines(ctx, cStdErr, "stderr")

		err = <-errC
		exitIf(err)
		err = <-errC
		exitIf(err)

		cmd.Wait()
	} else {
		// log is given through stdin
		go paintLines(ctx, os.Stdin, "stdout")

		err = <-errC
		exitIf(err)
	}
}
示例#4
0
文件: exec_test.go 项目: Zuozuohao/go
func helperCommandContext(t *testing.T, ctx context.Context, s ...string) (cmd *exec.Cmd) {
	testenv.MustHaveExec(t)

	cs := []string{"-test.run=TestHelperProcess", "--"}
	cs = append(cs, s...)
	if ctx != nil {
		cmd = exec.CommandContext(ctx, os.Args[0], cs...)
	} else {
		cmd = exec.Command(os.Args[0], cs...)
	}
	cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
	return cmd
}
示例#5
0
文件: main.go 项目: CodyGuo/Go-Cody
func main() {
	ctx, cancel := context.WithCancel(context.Background())
	cmd := exec.CommandContext(ctx, "./b")
	cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
	cmd.Stdout = os.Stdout
	cmd.Start()

	time.Sleep(10 * time.Second)
	fmt.Println("退出程序中...", cmd.Process.Pid)
	cancel()

	cmd.Wait()
}
示例#6
0
文件: cmd.go 项目: CodyGuo/Go-Cody
func (m *iManIP) DoCmd(cmd string) error {
	if len(cmd) == 0 {
		return errors.New("The cmd can't be empty!")
	}

	if m.ctx == nil {
		m.ctx = context.Background()
	}
	command := strings.Split(cmd, " ")
	m.Cmd = exec.CommandContext(m.ctx, command[0], command[1:]...)
	m.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}

	return nil
}
// fail because load didn't receive data from stdin
func (s *DockerSuite) TestLoadNoStdinFail(c *check.C) {
	pty, tty, err := pty.Open()
	c.Assert(err, check.IsNil)
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	cmd := exec.CommandContext(ctx, dockerBinary, "load")
	cmd.Stdin = tty
	cmd.Stdout = tty
	cmd.Stderr = tty
	c.Assert(cmd.Run(), check.NotNil) // docker-load should fail

	buf := make([]byte, 1024)

	n, err := pty.Read(buf)
	c.Assert(err, check.IsNil) //could not read tty output
	c.Assert(string(buf[:n]), checker.Contains, "requested load from stdin, but stdin is empty")
}
示例#8
0
文件: main.go 项目: qnib/go-ipfs
func main() {
	if len(os.Args) < 3 {
		fmt.Fprintf(os.Stderr,
			"Usage: %s <timeout-in-sec> <command ...>\n", os.Args[0])
		os.Exit(1)
	}
	timeout, err := strconv.ParseUint(os.Args[1], 10, 32)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		os.Exit(1)
	}
	ctx, _ := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)

	cmd := exec.CommandContext(ctx, os.Args[2], os.Args[3:]...)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err = cmd.Start()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
	}
	err = cmd.Wait()

	if err != nil {
		if ctx.Err() != nil {
			os.Exit(124)
		} else {
			exitErr, ok := err.(*exec.ExitError)
			if !ok {
				fmt.Fprintf(os.Stderr, "Error: %v\n", err)
				os.Exit(255)
			}
			waits, ok := exitErr.Sys().(syscall.WaitStatus)
			if !ok {
				fmt.Fprintf(os.Stderr, "Error: %v\n", err)
				os.Exit(255)
			}
			os.Exit(waits.ExitStatus())
		}
	} else {
		os.Exit(0)
	}
}
示例#9
0
// ExecWithTimeout runs an external command with a timeout.
// If the command times out the returned error will be a context.DeadlineExceeded error.
// If showOutput is true then output will be printed to stderr as well as returned.
// It returns the stdout only, combined stdout and stderr and any error that occurred.
func ExecWithTimeout(dir string, env []string, timeout time.Duration, defaultTimeout cli.Duration, showOutput bool, argv []string) ([]byte, []byte, error) {
	if timeout == 0 {
		timeout = time.Duration(defaultTimeout)
	}
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()
	cmd := exec.CommandContext(ctx, argv[0], argv[1:]...)
	cmd.Dir = dir
	cmd.Env = env

	var out bytes.Buffer
	var outerr safeBuffer
	if showOutput {
		cmd.Stdout = io.MultiWriter(os.Stderr, &out, &outerr)
		cmd.Stderr = io.MultiWriter(os.Stderr, &outerr)
	} else {
		cmd.Stdout = io.MultiWriter(&out, &outerr)
		cmd.Stderr = &outerr
	}
	err := cmd.Run()
	return out.Bytes(), outerr.Bytes(), err
}