aboutsummaryrefslogtreecommitdiffstats
path: root/util/process/process.go
blob: e3f47282254613fc759fd08205ebc841c617883b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package process

import (
	"os"
	"syscall"
)

// IsRunning tell is a process is running
func IsRunning(pid int) bool {
	// never return no error in a unix system
	process, err := os.FindProcess(pid)
	if err != nil {
		return false
	}

	// Signal 0 doesn't do anything but allow testing the process
	err = process.Signal(syscall.Signal(0))
	if err == nil {
		return true
	}
	if err.Error() == "os: process already finished" {
		return false
	}
	if errno, ok := err.(syscall.Errno); ok {
		switch errno {
		case syscall.ESRCH:
			return false
		case syscall.EPERM:
			return true
		}
	}
	return false
}