dstk51319 2013-12-12 15:54
浏览 150
已采纳

Golang执行:状态:文件移至该目录后没有此类文件或目录

I have a script that I am using for deployment using the "os/exec" package. One of the commands that I use is as follows:

cpInit = exec.Command("cp", "initScripts/nginx", "/etc/init.d/nginx")

and another:

startNginx = exec.Command("/etc/init.d/nginx", "start")

Initially I ran the first command with err := cpInit.Run(), but later when I run the second command I get the error:

exec: "/etc/init.d/nginx": stat /etc/init.d/nginx: no such file or directory

But when the program exits /etc/init.d/nginx is there, so I thought maybe the first command didn't finished (even though Run() waits until the command returns). I changed Run() to Start() and Wait() only to get the same results. Can anyone tell me why the second command can't find that file?

  • 写回答

1条回答 默认 最新

  • dongsi7759 2013-12-12 18:34
    关注

    When you run exec.Command(...) it immediately goes and checks for the file's existence, but has to defer the error until you call Run(), because the Command() call doesn't return an error.

    See here for the definition of Command: http://golang.org/src/pkg/os/exec/exec.go?s=3410:3455#L99

    It calls LookPath(...) defined here: http://golang.org/src/pkg/os/exec/lp_unix.go?s=902:944#L23

    You need to initialize the Command after you know the file is there--after calling Run() on your copy command.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站