Check if a process exists in go way

后端 未结 7 1879
甜味超标
甜味超标 2020-12-31 02:29

If I have the PID of a process, is os.FindProcess enough to test for the existing of the process? I mean if it returns err can I assume that it\'s terminated (o

7条回答
  •  感情败类
    2020-12-31 02:31

    On Windows checking the result of os.FindProcess() seems to be enough to check if process is running.

    func isProcessRunning(pid int) bool {
        _, err = os.FindProcess(pid)
        if err != nil {
            return false
        }
        if runtime.GOOS == "windows" {
            return true
        }
        return false // further checking for other systems then Windows is not supported here
    }
    

提交回复
热议问题