27

Situation:

I want to run a command that puts itself into the background. If it makes it more possible, then I'll run the command in foreground and bring it into the background by myself.

Question:

When the process runs in background: how can I get it's pid using Go?

I tried the following:

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 and leaves ssh running in the background. But it's pid is not the pid of the running ssh process. Moreover, it's the pid of the parent ssh process before it forked and backgrounded itself.

3
  • What do you mean by "background"? When you Start() your Cmd you already have a separate child process (with proper Process.Pid) running concurrently to your Go application. Maybe you just need to remove -f from ssh flags? Sep 15, 2016 at 12:25
  • 1
    I need the process to continue running after my go process exits. I then need to be able to kill the backgrounded ssh process in the future, for which I'll need to have stored its pid.
    – sbs
    Sep 15, 2016 at 13:18
  • Hmm, actually you're right, the process continues if I don't Wait for it and just exit.
    – sbs
    Sep 15, 2016 at 13:59

2 Answers 2

38

You don't need anything special, just don't tell ssh to background itself and don't Wait() for it. Example:

$ cat script.sh
#!/bin/sh
sleep 1
echo "I'm the script with pid $$"
for i in 1 2 3; do
        sleep 1
        echo "Still running $$"
done
$ cat proc.go
package main

import (
       "log"
       "os"
       "os/exec"
)

func main() {
     cmd := exec.Command("./script.sh")
     cmd.Stdout = os.Stdout
     err := cmd.Start()
     if err != nil {
        log.Fatal(err)
     }
     log.Printf("Just ran subprocess %d, exiting\n", cmd.Process.Pid)
}
$ go run proc.go
2016/09/15 17:01:03 Just ran subprocess 3794, exiting
$ I'm the script with pid 3794
Still running 3794
Still running 3794
Still running 3794
2
  • Hello, your example is working correctly but the process becomes in S mode after it finish. Is there a better way to achieve same results ? Where i can get a pid in order to check if finished. In my case i don't need the output as there will be log file containing the results already if finished successfully Nov 5, 2017 at 3:31
  • @Mostafa Hussein please what is S mode? Mar 26, 2022 at 9:59
5

@Mostafa Hussein, can use goroutine waiting, manage process

function main()
    cmd := exec.Command( "shell.sh" )
    err := cmd.Start()
    if err != nil {
        return err
    }
    pid := cmd.Process.Pid
    // use goroutine waiting, manage process
    // this is important, otherwise the process becomes in S mode
    go func() { 
        err = cmd.Wait()
        fmt.Printf("Command finished with error: %v", err)
    }()
    return nil
}
2
  • please what is S mode? Mar 11, 2022 at 21:55
  • He means "interruptible sleep" as shown by ps. It's also called a zombie process. Basically, it's waiting for the parent process to get its exit code, and if you don't wait for it, it'll stick around forever.
    – Niicodemus
    Jun 10, 2022 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.