J
J
jajabin2019-10-31 20:39:20
go
jajabin, 2019-10-31 20:39:20

How to run a program in the background by calling exec.Command?

I implemented an application that receives data on the port via UDP protocol, when I run this application from the console it works as it should, then I wrote an api to it, to turn it off, on and restart it, added it to the PATH variable, at the first POST request it starts, but the data does not accept if I send a request to turn it off and then it starts to receive data to turn it on, but only from one client and only once, then you need to repeat the on / off operation again, let me remind you that when you turn it on normally, not through api, everything works as it should. I run everything as root.

func start(e echo.Context) error {
  path, err := exec.LookPath(PARSERNAME)
  if err != nil {
    return err
  }
  cmd = exec.Command(path, "-p")
  err = cmd.Start()
  wg.Add(1)
  go func() {
    err = cmd.Wait()
    if err != nil {
      fmt.Printf("Err: %v\n", err)
    }
    wg.Done()
  }()

  return e.String(http.StatusOK,
    fmt.Sprintf("start %s\n",
      time.Now().Format("Jan _2 15:04:05")))
}

func stop(e echo.Context) error {
  if err := cmd.Process.Kill(); err != nil {
    fmt.Printf("Err: %v\n", err)
  }
  return e.String(http.StatusOK,
    fmt.Sprintf(" stop %s\n",
      time.Now().Format("Jan _2 15:04:05")))
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question