当前位置: 首页>>代码示例>>Golang>>正文


Golang Cmd.SysProcAttr方法代码示例

本文整理汇总了Golang中os/exec.Cmd.SysProcAttr方法的典型用法代码示例。如果您正苦于以下问题:Golang Cmd.SysProcAttr方法的具体用法?Golang Cmd.SysProcAttr怎么用?Golang Cmd.SysProcAttr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在os/exec.Cmd的用法示例。


在下文中一共展示了Cmd.SysProcAttr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Example

func Example() {
	var cmd *exec.Cmd
	memd := func(t *tcptest.TCPTest) {
		cmd = exec.Command("memcached", "-p", fmt.Sprintf("%d", t.Port()))
		cmd.SysProcAttr = &syscall.SysProcAttr{
			Setpgid: true,
		}

		go cmd.Run()
		for loop := true; loop; {
			select {
			case <-t.Done():
				cmd.Process.Kill()
				loop = false
			}
		}
	}

	server, err := tcptest.Start2(memd, 30*time.Second)
	if err != nil {
		log.Fatalf("Failed to start memcached: %s", err)
	}

	log.Printf("memcached started on port %d", server.Port())
	defer func() {
		if cmd != nil && cmd.Process != nil {
			cmd.Process.Signal(syscall.SIGTERM)
		}
	}()

	server.Stop()

	server.Wait()
}
开发者ID:okamuuu,项目名称:awesome-go,代码行数:34,代码来源:example_test.go

示例2: Start

func (r *RealCommandRunner) Start(cmd *exec.Cmd) error {
	if cmd.SysProcAttr == nil {
		cmd.SysProcAttr = &syscall.SysProcAttr{
			Setpgid: true,
		}
	} else {
		cmd.SysProcAttr.Setpgid = true
	}

	if r.debug {
		log.Printf("\x1b[40;36mspawning: %s\x1b[0m\n", prettyCommand(cmd))
		r.tee(cmd)
	}

	err := r.resolve(cmd).Start()

	if r.debug {
		if err != nil {
			log.Printf("\x1b[40;31mspawning failed: %s\x1b[0m\n", err)
		} else {
			log.Printf("\x1b[40;32mspawning succeeded\x1b[0m\n")
		}
	}

	return err
}
开发者ID:WIZARD-CXY,项目名称:golang-devops-stuff,代码行数:26,代码来源:command_runner.go

示例3: Hide

// Configures the given Command to produce a hidden window, if possible. Must
// be called before the Command is executed.
func Hide(c *exec.Cmd) {
	if c.SysProcAttr == nil {
		c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
	} else {
		c.SysProcAttr.HideWindow = true
	}
}
开发者ID:EricBurnett,项目名称:WebCmd,代码行数:9,代码来源:platform_windows.go

示例4: Launch

// Launch creates and begins debugging a new process. First entry in
// `cmd` is the program to run, and then rest are the arguments
// to be supplied to that process.
func Launch(cmd []string) (*Process, error) {
	var (
		proc *exec.Cmd
		err  error
	)
	// check that the argument to Launch is an executable file
	if fi, staterr := os.Stat(cmd[0]); staterr == nil && (fi.Mode()&0111) == 0 {
		return nil, NotExecutableErr
	}
	dbp := New(0)
	dbp.execPtraceFunc(func() {
		proc = exec.Command(cmd[0])
		proc.Args = cmd
		proc.Stdout = os.Stdout
		proc.Stderr = os.Stderr
		proc.SysProcAttr = &syscall.SysProcAttr{Ptrace: true, Setpgid: true}
		err = proc.Start()
	})
	if err != nil {
		return nil, err
	}
	dbp.Pid = proc.Process.Pid
	_, _, err = dbp.wait(proc.Process.Pid, 0)
	if err != nil {
		return nil, fmt.Errorf("waiting for target execve failed: %s", err)
	}
	return initializeDebugProcess(dbp, proc.Path, false)
}
开发者ID:alexbrainman,项目名称:delve,代码行数:31,代码来源:proc_linux.go

示例5: TestMemd

func TestMemd(t *testing.T) {
	var cmd *exec.Cmd
	memd := func(t *tcptest.TCPTest) {
		cmd = exec.Command("memcached", "-p", fmt.Sprintf("%d", t.Port()))
		cmd.SysProcAttr = &syscall.SysProcAttr{
			Setpgid: true,
		}
		cmd.Run()
	}

	server, err := tcptest.Start2(memd, 30*time.Second)
	if err != nil {
		log.Fatalf("Failed to start memcached: %s", err)
	}
	t.Logf("memcached started on port %d", server.Port())
	defer func() {
		if cmd != nil && cmd.Process != nil {
			cmd.Process.Signal(syscall.SIGTERM)
		}
	}()

	argv := []string{"-p", fmt.Sprintf("%d", server.Port()), "-k", "test"}
	ckr := run(argv)
	if ckr.Status.String() != "OK" {
		t.Errorf("faild to check memcache:%s", ckr)
	}
	cmd.Process.Signal(syscall.SIGTERM)
	server.Wait()
}
开发者ID:hiroakis,项目名称:go-check-plugins,代码行数:29,代码来源:check-memcached_test.go

示例6: start

func start(c *exec.Cmd) (pty *os.File, err error) {
	pty, tty, err := open()
	if err != nil {
		return nil, err
	}
	defer tty.Close()

	err = setEcho(pty, false)
	if err != nil {
		return nil, err
	}

	err = setEcho(tty, false)
	if err != nil {
		return nil, err
	}

	c.Stdout = tty
	c.Stdin = tty
	c.Stderr = tty
	if c.SysProcAttr == nil {
		c.SysProcAttr = &syscall.SysProcAttr{}
	}
	c.SysProcAttr.Setctty = true
	c.SysProcAttr.Setsid = true
	err = c.Start()
	if err != nil {
		pty.Close()
		return nil, err
	}

	return pty, err
}
开发者ID:xiqingping,项目名称:golibs,代码行数:33,代码来源:spawn_linux.go

示例7: Run

func (r *RealCommandRunner) Run(cmd *exec.Cmd) error {
	if cmd.SysProcAttr == nil {
		cmd.SysProcAttr = &syscall.SysProcAttr{
			Setpgid: true,
		}
	} else {
		cmd.SysProcAttr.Setpgid = true
	}

	if r.debug {
		log.Printf("\x1b[40;36mexecuting: %s\x1b[0m\n", prettyCommand(cmd))
		r.tee(cmd)
	}

	err := r.resolve(cmd).Run()

	if r.debug {
		if err != nil {
			log.Printf("\x1b[40;31mcommand failed (%s): %s\x1b[0m\n", prettyCommand(cmd), err)
		} else {
			log.Printf("\x1b[40;32mcommand succeeded (%s)\x1b[0m\n", prettyCommand(cmd))
		}
	}

	return err
}
开发者ID:WIZARD-CXY,项目名称:golang-devops-stuff,代码行数:26,代码来源:command_runner.go

示例8: runPtyCmd

func (pipe *Pipe) runPtyCmd(cmd *exec.Cmd) error {
	py, tty, err := pty.Open()
	if err != nil {
		return err
	}
	defer tty.Close()
	env := os.Environ()
	env = append(env, "TERM=xterm")
	cmd.Env = env
	cmd.Stdout = tty
	cmd.Stdin = tty
	cmd.Stderr = tty
	cmd.SysProcAttr = &syscall.SysProcAttr{Setctty: true, Setsid: true}
	ws := &Winsize{
		Width:  uint16(pipe.opts.Width),
		Height: uint16(pipe.opts.Height),
	}
	_, _, syserr := syscall.Syscall(syscall.SYS_IOCTL, py.Fd(), uintptr(syscall.TIOCSWINSZ), uintptr(unsafe.Pointer(ws)))
	if syserr != 0 {
		return syserr
	}
	pCmd := pipedCmd{
		pipe:   pipe,
		cmd:    cmd,
		stdin:  py,
		stdout: py,
		stderr: nil,
	}
	return pCmd.run()
}
开发者ID:eldarion-gondor,项目名称:gondor,代码行数:30,代码来源:server.go

示例9: spawnWithTty

func (w *Spawn) spawnWithTty(cmd *exec.Cmd) ([]*os.File, error) {
	pty, tty, err := w.PTY.Open()
	if err != nil {
		return nil, fmt.Errorf("container_daemon: open pipe: %s", err)
	}

	cmd.Stdin = tty
	cmd.Stdout = tty
	cmd.Stderr = tty

	if cmd.SysProcAttr == nil {
		cmd.SysProcAttr = &syscall.SysProcAttr{}
	}

	cmd.SysProcAttr.Setctty = true
	cmd.SysProcAttr.Setsid = true

	exitFd, err := wireExit(cmd, w.Runner)
	if err != nil {
		pty.Close()
		tty.Close()
		return nil, err
	}

	return []*os.File{pty, exitFd}, err
}
开发者ID:nagyistoce,项目名称:garden-linux,代码行数:26,代码来源:spawn.go

示例10: timedCommand

// timedCommand executes the given command, terminating it forcefully
// if it is still running after the given timeout elapses.
func (e *executor) timedCommand(timeout time.Duration, opts opts, command *exec.Cmd) error {
	// Make the process of this command a new process group leader
	// to facilitate clean up of processes that time out.
	command.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
	// Kill this process group explicitly when receiving SIGTERM
	// or SIGINT signals.
	sigchan := make(chan os.Signal, 1)
	signal.Notify(sigchan, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT)
	go func() {
		<-sigchan
		e.terminateProcessGroup(opts, command)
	}()
	if err := command.Start(); err != nil {
		e.printf(e.verboseStdout(opts), "FAILED: %v", err)
		return err
	}
	done := make(chan error, 1)
	go func() {
		done <- command.Wait()
	}()
	select {
	case <-time.After(timeout):
		// The command has timed out.
		e.terminateProcessGroup(opts, command)
		// Allow goroutine to exit.
		<-done
		e.printf(e.verboseStdout(opts), "TIMED OUT")
		return commandTimedOutErr
	case err := <-done:
		e.printf(e.verboseStdout(opts), okOrFailed(err))
		return err
	}
}
开发者ID:vanadium,项目名称:go.jiri,代码行数:35,代码来源:executor.go

示例11: start

func start(c *exec.Cmd) (io.ReadCloser, io.WriteCloser, error) {
	c.SysProcAttr = &syscall.SysProcAttr{}
	//c.SysProcAttr.CreationFlags = 16 // CREATE_NEW_CONSOLE

	stdin, err := c.StdinPipe()

	if err != nil {
		return nil, nil, err
	}

	stdout, err := c.StdoutPipe()

	if err != nil {
		return nil, nil, err
	}

	stderr, err := c.StderrPipe()

	if err != nil {
		return nil, nil, err
	}

	t := &term{}
	t.cmd = c
	t.stderr = stderr
	t.stdout = stdout
	t.stdin = stdin

	err = t.start()
	if err != nil {
		return nil, nil, err
	}

	return t, t, nil
}
开发者ID:XQYCHJ,项目名称:godev,代码行数:35,代码来源:terminal-windows.go

示例12: PtyRun

//PtyRun assigns a psuedo terminal tty to the corresponding std.io set and returns an error indicating state
func PtyRun(c *exec.Cmd, tty *os.File) (err error) {
	defer tty.Close()
	c.Stdout = tty
	c.Stdin = tty
	c.Stderr = tty
	c.SysProcAttr = &syscall.SysProcAttr{Setctty: true, Setsid: true}
	return c.Start()
}
开发者ID:influx6,项目名称:goproj,代码行数:9,代码来源:sshprotocol.go

示例13: PutInBackgroundIfLinux

func PutInBackgroundIfLinux(cmd *exec.Cmd) {
	// To put this in the background, we have to change the process group
	//	per: https://groups.google.com/forum/#!topic/golang-nuts/shST-SDqIp4.
	//	But it doesn't work on Windows
	cmd.SysProcAttr = &syscall.SysProcAttr{
		Setpgid: true,
	}
}
开发者ID:cretz,项目名称:systrument,代码行数:8,代码来源:shell_linux.go

示例14: setChroot

func (e *ExecScriptCheck) setChroot(cmd *exec.Cmd) {
	if e.FSIsolation {
		if cmd.SysProcAttr == nil {
			cmd.SysProcAttr = &syscall.SysProcAttr{}
		}
		cmd.SysProcAttr.Chroot = e.taskDir
	}
	cmd.Dir = "/"
}
开发者ID:PagerDuty,项目名称:nomad,代码行数:9,代码来源:checks_unix.go

示例15: SetCmd

func (p *Pty) SetCmd(c *exec.Cmd) {
	c.Stdin = p.tty
	c.Stdout = p.tty
	c.Stderr = p.tty
	if c.SysProcAttr == nil {
		c.SysProcAttr = &syscall.SysProcAttr{}
	}
	c.SysProcAttr.Setctty = true
	c.SysProcAttr.Setsid = true
}
开发者ID:chzyer,项目名称:temux,代码行数:10,代码来源:term.go


注:本文中的os/exec.Cmd.SysProcAttr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。