Golang program exits after calling linux shell command
I am running the following golang (snippet) program with root privileges:
binary, lookErr := exec.LookPath("auditctl")
if lookErr != nil {
panic(lookErr)
}
env := os.Environ()
args := []string{"auditctl", "-D"}
execErr := syscall.Exec(binary, args, env)
if execErr != nil {
fmt.Println("error")
panic(execErr)
}
fmt.Println("no error")
Since I don't have any audit rules on the system, the command prints the following to the terminal. This is normal, as if typing directly into the shell.
No rules
Except that neither "error" nor "error" is printed. This means that the golang program exits right after syscall.Exec. How did this happen and how can I continue execution after syscall.Exec, because I have other possibilities to run within the same program.
source to share
syscall.Exec
callsexecve(2)
, which on linux does not return execution to the caller, but replaces the current (Go) process with the called process.
As David Budworth, and mkopriva suggest .. if you really want to create a separate process and return to your Go code after spawning; consider usingexec.Command
source to share