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.

+3


source to share


2 answers


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

+8


source


As @mkopriva mentioned you probably want exec.Cmd

here's an example:



cmd := exec.Command("auditctl", "-D")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = os.Environ()
if err := cmd.Run(); err != nil {
    log.Println("Failed to run auditctl:",err)
} 

      

+2


source







All Articles