本文整理汇总了Golang中exec.Run函数的典型用法代码### 示例。如果您正苦于以下问题:Golang Run函数的具体用法?Golang Run怎么用?Golang Run使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。

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

示例1: run

// run runs the command argv, resolving argv[0] if necessary by searching $PATH.
// It provides input on standard input to the command.
func run(argv []string, input []byte) (out string, err os.Error) {
    if len(argv) < 1 {
        err = os.EINVAL
        goto Error
    }
    prog, ok := lookPathCache[argv[0]]
    if !ok {
        prog, err = exec.LookPath(argv[0])
        if err != nil {
            goto Error
        }
        lookPathCache[argv[0]] = prog
    }
    // fmt.Fprintf(os.Stderr, "%v\n", argv);
    var cmd *exec.Cmd
    if len(input) == 0 {
        cmd, err = exec.Run(prog, argv, os.Environ(), "", exec.DevNull, exec.Pipe, exec.MergeWithStdout)
        if err != nil {
            goto Error
        }
    } else {
        cmd, err = exec.Run(prog, argv, os.Environ(), "", exec.Pipe, exec.Pipe, exec.MergeWithStdout)
        if err != nil {
            goto Error
        }
        go func() {
            cmd.Stdin.Write(input)
            cmd.Stdin.Close()
        }()
    }
    defer cmd.Close()
    var buf bytes.Buffer
    _, err = io.Copy(&buf, cmd.Stdout)
    out = buf.String()
    if err != nil {
        cmd.Wait(0)
        goto Error
    }
    w, err := cmd.Wait(0)
    if err != nil {
        goto Error
    }
    if !w.Exited() || w.ExitStatus() != 0 {
        err = w
        goto Error
    }
    return

Error:
    err = &runError{copy(argv), err}
    return
}

开发者ID:ivanwyc,项目名称:google-go,代码行数:54,代码来源:main.go

示例2: main

func main() {
    ts, e := os.Open("tests", os.O_RDONLY, 0)
    error.FailOn(e)
    error.FailOn(os.RemoveAll("tests/tmp"))
    ds, e := ts.Readdir(-1)
    error.FailOn(e)
    // Set up an environment with an appropriate path
    wd, e := os.Getwd()
    error.FailOn(e)
    os.Setenv("PATH", path.Join(wd, "bin")+":"+os.Getenv("PATH"))
    for _, d := range ds {
        if d.IsRegular() && (d.Permission()&1) == 1 && endsWith(d.Name, ".sh") {
            basename := d.Name[0 : len(d.Name)-3]
            fmt.Printf("Running %s... ", basename)
            dirname := path.Join("tests/tmp", basename)
            error.FailOn(os.MkdirAll(dirname, 0777))
            pid, e := exec.Run(path.Join(wd, "tests", d.Name), []string{}, os.Environ(), dirname,
                exec.DevNull, exec.Pipe, exec.MergeWithStdout)
            error.FailOn(e)
            o, e := ioutil.ReadAll(pid.Stdout)
            error.FailOn(e)
            ret, e := pid.Wait(0)
            error.FailOn(e)
            if ret.ExitStatus() != 0 {
                error.Print("FAILED!\n", string(o))
                error.Print("Test '", basename, "' failed!")
                exit.Exit(1)
            }
            fmt.Println("passed.")
        }
    }
}

开发者ID:droundy,项目名称:iolaus,代码行数:32,代码来源:harness.go

示例3: runShellCommand

func runShellCommand(argv []string) map[string]string {
    result := map[string]string{
        "out": "",
        "err": ""}

    app := argv[0]
    home := fmt.Sprintf("HOME=%v", os.Getenv("HOME"))
    path := fmt.Sprintf("PATH=%v", os.Getenv("PATH"))
    env := []string{home, path}

    cmd, err := exec.Run(app, argv, env, "", exec.DevNull, exec.Pipe, exec.Pipe)

    if err != nil {
        fmt.Fprintln(os.Stderr, err.String())
        result["err"] = err.String()
        return result
    }

    var b1 bytes.Buffer
    io.Copy(&b1, cmd.Stdout)
    fmt.Println(b1.String())
    result["out"] = b1.String()

    var b2 bytes.Buffer
    io.Copy(&b2, cmd.Stderr)
    fmt.Println(b2.String())
    result["err"] = b2.String()

    cmd.Close()

    return result
}

开发者ID:nicneo925,项目名称:appscale,代码行数:32,代码来源:neptune.go

示例4: justrun

func justrun(cmd string, args ...string) os.Error {
    abscmd, err := exec.LookPath(cmd)
    if err != nil {
        return os.NewError("Couldn't find " + cmd + ": " + err.String())
    }

    cmdargs := make([]string, len(args)+1)
    cmdargs[0] = cmd
    for i, a := range args {
        cmdargs[i+1] = a
    }
    pid, err := exec.Run(abscmd, cmdargs, nil, "",
        exec.PassThrough, exec.PassThrough, exec.PassThrough)
    if err != nil {
        return err
    }
    wmsg, err := pid.Wait(0)
    if err != nil {
        return err
    }
    if wmsg.ExitStatus() != 0 {
        return os.NewError(cmd + " exited with status " + fmt.Sprint(wmsg.ExitStatus()))
    }
    return nil
}

开发者ID:droundy,项目名称:go-crazy,代码行数:25,代码来源:go-crazy.go

示例5: Stop

func (p *ProcessStarter) Stop(com *Command, reply *string) os.Error {
    fmt.Printf("try stopping process\n")
    data, ok := processMap[com.name]
    if !ok {
        fmt.Printf("server unknown:%s", com.name)
    } else {
        if data.cmd != nil && data.cmd.Pid > 0 {
            fmt.Printf("kill pid server %d:", data.cmd.Pid)
            var pid string = fmt.Sprintf("%d", data.cmd.Pid)
            println(pid)
            args := []string{com.command, "--stop", pid}
            env := []string{"DYLD_LIBRARY_PATH=/Users/jholscher/Documents/bripper/install/lib/"}
            _, err := exec.Run(com.command, args, env, com.dir, exec.DevNull, exec.Pipe, exec.PassThrough)
            if err != nil {
                fmt.Println("error stop process")
            } else {
                //fmt.Println("process stopped")
            }

            //      syscall.Kill(data.cmd.Pid, 15)
        } else {
            fmt.Printf("failed to stop the process, because one of the following data is nil or lower 0 [%s,%d]", data.cmd, data.cmd.Pid)
        }
    }

    return nil
}

开发者ID:psychobob666,项目名称:MediaEncodingCluster,代码行数:27,代码来源:mainrpc.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng