I want to run a command that is placed in the background (or, if this makes things more possible, run the command in the foreground and remember it yourself) and get the pid of the background process. How to do it in Go? I tried:
cmd := exec.Command("ssh", "-i", keyFile, "-o", "ExitOnForwardFailure yes", "-fqnNTL", fmt.Sprintf("%d:127.0.0.1:%d", port, port), fmt.Sprintf("% s@ %s", serverUser, serverIP)) cmd.Start() pid := cmd.Process.Pid cmd.Wait()
This returns ~ instantly, leaves ssh in the background, but the pid is not the pid of the current ssh process (presumably this is the pid of the ssh parent process before it branches and comment out itself).
How to do it right?
source share