本文整理汇总了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()
}
示例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
}
示例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
}
}
示例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)
}
示例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()
}
示例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
}
示例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
}
示例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()
}
示例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
}
示例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
}
}
示例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
}
示例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()
}
示例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,
}
}
示例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 = "/"
}
示例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
}