Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top