I was reading about exec in Go https://gobyexample.com/execing-processes, and tried to do the same using goroutines.
In the following code, I'm trying to make Go run ls
, then print a success message in the main thread. However, it's only printing the ls, but not the success message.
What's going on?
Thanks.
package main
import "syscall"
import "os"
import "os/exec"
import "fmt"
func main() {
p := fmt.Println
done := make(chan bool)
binary, lookErr := exec.LookPath("ls")
if lookErr != nil {
panic(lookErr)
}
args := []string{"ls", "-a", "-l", "-h"}
env := os.Environ()
go func() {
execErr := syscall.Exec(binary, args, env)
if execErr != nil {
panic(execErr)
}
done <- true
}()
<-done
p("Done with exec")
}
Here's the output:
Valeriys-MacBook-Pro:test valeriy$ go run test.go
total 8
drwxr-xr-x 3 valeriy staff 96B Dec 17 15:46 .
drwxr-xr-x 8 valeriy staff 256B Dec 17 00:06 ..
-rw-r--r-- 1 valeriy staff 433B Dec 17 15:38 test.go
syscall.Exec
replaces the current process with the one invoked.
If you want to run an external command while keeping the original program running, you need to use exec.Command
By the way, the link you included does say:
Sometimes we just want to completely replace the current Go process with another (perhaps non-Go) one.
If you really want to use the syscall
package, you can use syscall.StartProcess
which does a fork/exec as opposed to a plain exec.