golang exec后台进程并获取其pid

golang exec background process and get its pid

情况:

我想要 运行 一个将自身置于后台的命令。如果它更可能,那么我会 运行 前台的命令并自己将其带入后台。

问题:

当进程 运行 在后台运行时:我如何使用 Go 获取它 pid

我尝试了以下方法:

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()

此 returns 立即并在后台留下 ssh 运行ning。但它的pid并不是运行宁ssh进程的pid。此外,它是父 ssh 进程的 pid 在它分叉和后台运行之前。

您不需要任何特殊的东西,只是不要让 ssh 自己进入后台,也不要 Wait() 它。示例:

$ 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

@Mostafa Hussein,可以使用goroutine等待,管理进程

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
}