等待一个非子进程结束

Hi I am working on a simple code that will monitor a process and restart the process in case the process crashes. I have written a small sample code.

This is my external process

package main

import (
    "fmt"
    "time"
)

func main() {
    for {
        time.Sleep(1000 * time.Millisecond)
        fmt.Println("hello")
    }
}

This is the code that monitors it.

package main

import (
    "fmt"
    "os"
)

func main() {
    p, e := os.FindProcess(<processid>)
    fmt.Println(e)
    fmt.Println(p.Wait())
    fmt.Println("done")
}

The challenge here is that since the first process is not a child process of the second one, it does not wait and directly exits. Please let me know if anyone has any ideas around this.

Thanks.

Monitoring process exits because p.Wait() does not block.

From the docs:

On most operating systems, the Process must be a child of the current process or an error will be returned.

You can perhaps poll the process pool to check if the process still exists.