进行构建和exec:fork/exec:权限被拒绝 [英] Go build & exec: fork/exec: permission denied
本文介绍了进行构建和exec:fork/exec:权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用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
OS:OSX 10.11
解决方案
您正在将命令路径从go
二进制文件的位置更改为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
如果要更改工作目录,请使用 Cmd.Dir
>
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
解决方案
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
这篇关于进行构建和exec:fork/exec:权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文