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.

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

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭