本文整理汇总了Golang中os/exec.Cmd.Wait方法的典型用法代码示例。如果您正苦于以下问题:Golang Cmd.Wait方法的具体用法?Golang Cmd.Wait怎么用?Golang Cmd.Wait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os/exec.Cmd
的用法示例。
在下文中一共展示了Cmd.Wait方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Run
// Run runs the command and returns a channel of output lines, errors and result of cmd.Wait
func Run(cmd *exec.Cmd, lines chan string) (error, error) {
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
err = cmd.Start()
if err != nil {
return nil, err
}
var wg sync.WaitGroup
errCh := make(chan error, 2)
wg.Add(2)
go tailReader(bufio.NewReader(stdout), lines, errCh, &wg)
go tailReader(bufio.NewReader(stderr), lines, errCh, &wg)
wg.Wait()
select {
case err := <-errCh:
return nil, err
default:
}
return cmd.Wait(), nil
}
示例2: reloadHAproxy
func (this *Start) reloadHAproxy() (err error) {
var cmd *exec.Cmd = nil
waitStartCh := make(chan struct{})
if this.starting {
log.Info("haproxy starting")
cmd = exec.Command(this.command, "-f", configFile) // TODO use absolute path
this.starting = false
go func() {
<-waitStartCh
log.Info("haproxy started")
if err := cmd.Wait(); err != nil {
log.Error("haproxy: %v", err)
}
}()
} else {
shellScript := fmt.Sprintf("%s -f %s/%s -sf `cat %s/%s`",
this.command, this.root, configFile, this.root, haproxyPidFile)
log.Info("haproxy reloading: %s", shellScript)
cmd = exec.Command("/bin/sh", "-c", shellScript)
go func() {
<-waitStartCh
log.Info("haproxy reloaded")
if err := cmd.Wait(); err != nil {
log.Error("haproxy: %v", err)
}
}()
}
if err = cmd.Start(); err == nil {
waitStartCh <- struct{}{}
}
return err
}
示例3: send
// send produces a binary diff stream and passes it to cont
func send(repo, commit string, cont func(io.Reader) error) error {
parent := GetMeta(path.Join(repo, commit), "parent")
var cmd *exec.Cmd
if parent == "" {
cmd = exec.Command("btrfs", "send", FilePath(path.Join(repo, commit)))
} else {
cmd = exec.Command("btrfs", "send", "-p", FilePath(path.Join(repo, parent)), FilePath(path.Join(repo, commit)))
}
stdin, stdout := io.Pipe()
stderr := bytes.NewBuffer(nil)
cmd.Stdout = stdout
if err := cmd.Start(); err != nil {
return err
}
errC := make(chan error, 1)
go func() {
errC <- cont(stdin)
}()
waitErr := cmd.Wait()
closeErr := stdout.Close()
if waitErr != nil {
return fmt.Errorf("%s %s", waitErr.Error(), stderr.String())
}
if closeErr != nil {
return closeErr
}
return <-errC
}
示例4: cmdStream
// cmdStream executes a command, and returns its stdout as a stream.
// If the command fails to run or doesn't complete successfully, an error
// will be returned, including anything written on stderr.
func cmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, <-chan struct{}, error) {
chdone := make(chan struct{})
cmd.Stdin = input
pipeR, pipeW := io.Pipe()
cmd.Stdout = pipeW
var errBuf bytes.Buffer
cmd.Stderr = &errBuf
// Run the command and return the pipe
if err := cmd.Start(); err != nil {
return nil, nil, err
}
// Copy stdout to the returned pipe
go func() {
if err := cmd.Wait(); err != nil {
pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errBuf.String()))
} else {
pipeW.Close()
}
close(chdone)
}()
return pipeR, chdone, nil
}
示例5: writeGitRepositoryArchive
func writeGitRepositoryArchive(w io.Writer, path string, ref GitCommitRef) error {
var cmd *exec.Cmd
// TODO: Stream as tar with gzip
if ref == EmptyGitCommitRef {
cmd = exec.Command("/usr/bin/git", "archive", "--format", "zip", "master")
} else {
cmd = exec.Command("/usr/bin/git", "archive", "--format", "zip", string(ref))
}
cmd.Env = []string{}
cmd.Dir = path
var stderr bytes.Buffer
cmd.Stderr = utils.LimitWriter(&stderr, 20*1024)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
io.Copy(w, stdout)
if err := cmd.Wait(); err != nil {
return errors.New(fmt.Sprintf("Failed to archive repository: %s\n", err.Error()) + stderr.String())
}
return nil
}
示例6: CmdRunWithTimeout
func (w *Worker) CmdRunWithTimeout(cmd *exec.Cmd, timeout time.Duration) (error, bool) {
done := make(chan error)
go func() {
done <- cmd.Wait()
}()
var err error
select {
case <-time.After(timeout):
// timeout
if err = cmd.Process.Kill(); err != nil {
golog.Error("worker", "CmdRunTimeout", "kill error", 0,
"path", cmd.Path,
"error", err.Error(),
)
}
golog.Info("worker", "CmdRunWithTimeout", "kill process", 0,
"path", cmd.Path,
"error", errors.ErrExecTimeout.Error(),
)
go func() {
<-done // allow goroutine to exit
}()
return errors.ErrExecTimeout, true
case err = <-done:
return err, false
}
}
示例7: waitSimple
func waitSimple(command *Command, cmd *osexec.Cmd) error {
err := cmd.Wait()
if err != nil {
glog.Errorf("Command exited with %s: %s", err, DebugString(command))
}
return err
}
示例8: Run
func (c Command) Run(s HookState) error {
b, err := json.Marshal(s)
if err != nil {
return err
}
cmd := exec.Cmd{
Path: c.Path,
Args: c.Args,
Env: c.Env,
Stdin: bytes.NewReader(b),
}
errC := make(chan error, 1)
go func() {
out, err := cmd.CombinedOutput()
if err != nil {
err = fmt.Errorf("%s: %s", err, out)
}
errC <- err
}()
if c.Timeout != nil {
select {
case err := <-errC:
return err
case <-time.After(*c.Timeout):
cmd.Process.Kill()
cmd.Wait()
return fmt.Errorf("hook ran past specified timeout of %.1fs", c.Timeout.Seconds())
}
}
return <-errC
}
示例9: Publish
// Publish prints the result of a check to stdout.
func (p *ExecPublisher) Publish(result *CheckResult) error {
var cmd *exec.Cmd
if len(p.cmd) > 1 {
cmd = exec.Command(p.cmd[0], p.cmd[1:]...)
} else {
cmd = exec.Command(p.cmd[0])
}
var b bytes.Buffer
cmd.Stdout = &b
cmd.Stderr = &b
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
if err = cmd.Start(); err != nil {
p.log.Error("Exec failed", "error", err)
return err
}
if err = p.stdinTemplate.Execute(stdin, result); err != nil {
p.log.Error("Failed to write template data to stdin", "error", err)
return err
}
stdin.Close()
if err = cmd.Wait(); err != nil {
p.log.Error("Exec failed", "error", err, "output", b.String())
return err
}
return nil
}
示例10: runCommand
// runCommand runs the command with the given timeout, and returns any errors encountered and whether
// the command timed out or not
func runCommand(cmd *exec.Cmd, timeout time.Duration) (error, bool) {
out := make(chan error)
go func() {
if err := cmd.Start(); err != nil {
glog.V(4).Infof("Error starting execution: %v", err)
}
out <- cmd.Wait()
}()
if timeout == noCommandTimeout {
select {
case err := <-out:
if err != nil {
glog.V(4).Infof("Error executing command: %v", err)
}
return err, false
}
} else {
select {
case err := <-out:
if err != nil {
glog.V(4).Infof("Error executing command: %v", err)
}
return err, false
case <-time.After(timeout):
glog.V(4).Infof("Command execution timed out after %s", timeout)
return nil, true
}
}
}
示例11: callCont
func callCont(c *exec.Cmd, cont func(io.Reader) error) error {
_, callerFile, callerLine, _ := runtime.Caller(1)
log.Printf("%15s:%.3d -> %s", path.Base(callerFile), callerLine, strings.Join(c.Args, " "))
var reader io.Reader
var err error
if cont != nil {
reader, err = c.StdoutPipe()
if err != nil {
return err
}
}
stderr, err := c.StderrPipe()
if err != nil {
return err
}
if err = c.Start(); err != nil {
return err
}
if cont != nil {
if err = cont(reader); err != nil {
return err
}
}
buffer := bytes.NewBuffer(nil)
buffer.ReadFrom(stderr)
if buffer.Len() != 0 {
log.Print("Command had output on stderr.\n Cmd: ", strings.Join(c.Args, " "), "\nstderr: ", buffer)
}
return c.Wait()
}
示例12: RunCommandWithStdoutStderr
// RunCommandWithStdoutStderr execs a command and returns its output.
func RunCommandWithStdoutStderr(cmd *exec.Cmd) (bytes.Buffer, bytes.Buffer, error) {
var stdout, stderr bytes.Buffer
stderrPipe, err := cmd.StderrPipe()
stdoutPipe, err := cmd.StdoutPipe()
cmd.Env = os.Environ()
if err != nil {
fmt.Println("error at io pipes")
}
err = cmd.Start()
if err != nil {
fmt.Println("error at command start")
}
go func() {
streamOutput(stdoutPipe, &stdout, os.Stdout)
}()
go func() {
streamOutput(stderrPipe, &stderr, os.Stderr)
}()
err = cmd.Wait()
if err != nil {
fmt.Println("error at command wait")
}
return stdout, stderr, err
}
示例13: WaitCode
// WaitCode gets the exit code for a running command
//
// Normally returns exit code, nil
//
// Error Conditions:
// WaitCode takes err, so that it can be used on the result of Run{Cmd/Bash}.
// All failures to run the process manifest as an exit code of -1, with additional error info
func WaitCode(c *exec.Cmd, e error) (int, error) {
if c == nil {
return -1, e
}
return getCode(c.Wait())
}
示例14: rsyncWebsocket
func rsyncWebsocket(cmd *exec.Cmd, conn *websocket.Conn) error {
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
shared.WebsocketMirror(conn, stdin, stdout)
data, err2 := ioutil.ReadAll(stderr)
if err2 != nil {
shared.Debugf("error reading rsync stderr: %s", err2)
return err2
}
shared.Debugf("Stderr from rsync: %s", data)
err = cmd.Wait()
if err != nil {
shared.Debugf("rsync recv error %s: %s", err, string(data))
}
return err
}
示例15: execCommand
func execCommand(cmd *exec.Cmd, prefix string, name string) (bool, *string, *string) {
out, err := cmd.StdoutPipe()
outE, errE := cmd.StderrPipe()
if err != nil {
log.Warnf("[command] %s err: %s", prefix, out)
log.Warnf("[command] %s err: %s", prefix, err)
return false, nil, nil
}
if errE != nil {
log.Warnf("[command] %s err: %s", prefix, outE)
log.Warnf("[command] %s err: %s", prefix, errE)
return false, nil, nil
}
err = cmd.Start()
if err != nil {
log.Warnf("[command] %s err: %s", prefix, err)
return false, nil, nil
}
outResult := copyStream(out, prefix, name)
errResult := copyStream(outE, prefix, name)
err = cmd.Wait()
if err != nil {
log.Warnf("[command] %s err: %s", prefix, err)
return false, &outResult, &errResult
}
return true, &outResult, &errResult
}