登录
首页 >  Golang >  Go问答

代码出现"exec:未找到"的错误原因是什么?

来源:stackoverflow

时间:2024-02-14 11:00:23 153浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《代码出现"exec:未找到"的错误原因是什么?》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

问题内容

这是我的代码(writefromprocesstofilewithmax 是一个内部函数,并且工作正常):

// Go routines for non-blocking reading of stdout and stderr and writing to files
    g := new(errgroup.Group)

    // Read stdout in goroutine.
    g.Go(func() error {
        err = writeFromProcessToFileWithMax(stdoutScanner, stdoutFileWriter, maxStdoutFileLengthInGB)
        if err != nil {
            log.Error().Err(err).Msgf("Error writing to stdout file: %s", stdoutFilename)
            return err
        }
        return nil
    })

    // Read stderr in goroutine.
    g.Go(func() error {
        err = writeFromProcessToFileWithMax(stderrScanner, stderrFileWriter, maxStderrFileLengthInGB)
        if err != nil {
            log.Error().Err(err).Msgf("Error writing to stderr file: %s", stderrFilename)
            return err
        }
        return nil
    })

    // Wait the command in a goroutine.
    g.Go(func() error {
        return cmd.Wait()
    })

    // Starting the command
    if err = cmd.Start(); err != nil {
        log.Error().Err(err).Msg("Error starting command")
        return err
    }

    // Waiting until errorGroups groups are done
    if err = g.Wait(); err != nil {
        log.Error().Err(err).Msg("Error during running of the command")
    }

当我运行它时,在运行命令期间出现以下错误= error error =“exec:未启动”。但一切正常。

它会回来咬我还是我应该压制?


正确答案


在启动之前,您正在等待 cmd。在旧代码中,cmd.wait() 将在 cmd.start() 大部分时间之前调用。 (无法保证两个不同 goroutine 中的事情何时准确地发生,除非您明确使用同步点)

交换 goroutine 内 cmd.start()cmd.wait() 的顺序:

// Starting the command
if err = cmd.Start(); err != nil {
    log.Error().Err(err).Msg("Error starting command")
    return err
}

// Wait the command in a goroutine.
g.Go(func() error {
    return cmd.Wait()
})

当您启动在启动命令后等待的 goroutine 时,保证您以正确的顺序执行 cmd.start()cmd.wait()

至于为什么它似乎有效:g.wait()blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them.

因此,所有 go 例程都已完成,包括复制输出的例程,然后您会看到执行 cmd.wait() 的例程中的错误。

本篇关于《代码出现"exec:未找到"的错误原因是什么?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>