Go build & exec: fork/exec: permission denied

huangapple go评论93阅读模式
英文:

Go build & exec: fork/exec: permission denied

问题

我需要使用Go工具链构建一个程序,然后执行它。由于分叉导致权限错误,我想知道是否有办法绕过这个错误或者有什么最佳实践?我认为我的程序与Go test工具类似,尽管go test没有出现这种错误。

package main

import (
	"os"
	"os/exec"
	"flag"
	log "github.com/golang/glog"
)

func main() {
	flag.Parse()
	tdir := "abc"
	if err := os.MkdirAll(tdir, 0777); err != nil {
		log.Error(err)
		return
	}
	f, err := os.Create(tdir + "/main.go")
	if err != nil {
		log.Error(err)
		return
	}
	if err = f.Chmod(0777); err != nil {
		log.Error(err)
		return
	}
	defer f.Close()
	defer os.Remove(f.Name())
	if _, err = f.Write([]byte(tpl)); err != nil {
		log.Error(err)
		return
	}
	cmd := exec.Command("go", "build", "-o", "edoc")
	cmd.Path = tdir
	b, err := cmd.CombinedOutput()
	if err != nil {
		log.Errorf("%s, err %v", b, err)
		return
	}
}

var tpl = `package main

import (
	"fmt"
	"flag"
)

func main() {
	flag.Parse()
	fmt.Printf("Hello World")
}
`

错误信息:

E0202 18:24:42.359008   13600 main.go:36] , err fork/exec abc: permission denied

操作系统:OSX 10.11

英文:

I need to build a program using the Go toolchain and then execute it. For some reasons I get a permission error due the forking. Is there a way to circumvent this error or any best practice? I think my program does something similar with Go test tool, though go test doesn't get this kind of error.

package main

import(
	"os"
	"os/exec"
	"flag"
	log "github.com/golang/glog"
)

func main(){
	flag.Parse()
	tdir := "abc"
	if err := os.MkdirAll(tdir, 0777); err !=nil{
		log.Error(err)
		return
	}
	f, err := os.Create(tdir + "/main.go")
	if err !=nil{
		log.Error(err)
		return
	}
	if err = f.Chmod(0777); err !=nil{
		log.Error(err)
		return
	}
	defer f.Close()
	defer os.Remove(f.Name())
	if _, err = f.Write([]byte(tpl)); err !=nil{
		log.Error(err)
		return
	}
	cmd := exec.Command("go", "build", "-o", "edoc")
	cmd.Path = tdir
	b, err := cmd.CombinedOutput()
	if err !=nil{
		log.Errorf("%s, err %v", b, err)
		return
	}

}

var tpl = `package main

import(
	"fmt"
	"flag"
)

func main(){
	flag.Parse()
	fmt.Printf("Hello World")

}`

Error:

E0202 18:24:42.359008   13600 main.go:36] , err fork/exec abc: permission denied

OS: OSX 10.11

答案1

得分: 4

你正在将命令路径从go二进制文件的位置更改为abc

type Cmd struct {
    // Path是要运行的命令的路径。
    //
    // 这是唯一必须设置为非零值的字段。如果Path是相对路径,则相对于Dir进行评估。
    Path string

如果你想要改变工作目录,请使用Cmd.Dir

英文:

You're changing the command path from the location of your go binary, to abc.

> type Cmd struct {
> // Path is the path of the command to run.
> //
> // This is the only field that must be set to a non-zero
> // value. If Path is relative, it is evaluated relative
> // to Dir.
> Path string

If you want to change the working directory, use Cmd.Dir

huangapple
  • 本文由 发表于 2016年2月3日 00:46:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/35159117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定