gpt4 book ai didi

go - 等待非子进程结束

转载 作者:数据小太阳 更新时间:2023-10-29 03:24:38 24 4
gpt4 key购买 nike

您好,我正在编写一个简单的代码,它将监视进程并在进程崩溃时重新启动进程。我写了一个小示例代码。

这是我的外部进程

package main

import (
"fmt"
"time"
)

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

这是监控它的代码。

package main

import (
"fmt"
"os"
)

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

这里的挑战在于,由于第一个进程不是第二个进程的子进程,它不等待直接退出。如果有人对此有任何想法,请告诉我。

谢谢。

最佳答案

监控进程退出,因为p.Wait()没有阻塞。

来自docs :

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

您或许可以轮询进程池以检查进程是否仍然存在。

关于go - 等待非子进程结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45368284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com