2016-09-15 09:53
浏览 1.1k

golang exec后台进程并获取其pid


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.


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:", port, port), fmt.Sprintf("%s@%s", serverUser, serverIP))
pid := cmd.Process.Pid

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.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douhao7677 2016-09-15 14:02

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

    $ cat
    sleep 1
    echo "I'm the script with pid $$"
    for i in 1 2 3; do
            sleep 1
            echo "Still running $$"
    $ cat proc.go
    package main
    import (
    func main() {
         cmd := exec.Command("./")
         cmd.Stdout = os.Stdout
         err := cmd.Start()
         if err != nil {
         log.Printf("Just ran subprocess %d, exiting
    ", 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
    解决 无用
    打赏 举报
  • dqwh1203 2018-10-11 19:38

    @Mostafa Hussein, can use goroutine waiting, manage process

    function main()
        cmd := exec.Command( "" )
        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
    解决 无用
    打赏 举报

相关推荐 更多相似问题