From 95b1d03ffc83388f0088d91d72607af5f3ff2b90 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Fri, 12 Nov 2021 14:49:37 +0800 Subject: [PATCH] update --- go.mod | 7 +++++++ go.sum | 8 ++++++++ process.go | 31 +++++++++++++++++++++++++++++++ process_test.go | 10 ++++++++++ 4 files changed, 56 insertions(+) create mode 100644 go.sum create mode 100644 process.go create mode 100644 process_test.go diff --git a/go.mod b/go.mod index 2554adb..33e52fc 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ module kumoly.io/lib/stat go 1.17 + +require kumoly.io/lib/klog v0.0.8 + +require ( + github.com/mattn/go-isatty v0.0.14 // indirect + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a6631d5 --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +kumoly.io/lib/klog v0.0.8 h1:6hTfDlZh7KGnPrd2tUrauCKRImSnyyN9DHXpey3Czn8= +kumoly.io/lib/klog v0.0.8/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M= +kumoly.io/lib/klog v0.1.10 h1:GJxwcsUct8nF3oHtsJPTIlPKTUeB/+7jsbgh3bMvKMc= +kumoly.io/lib/klog v0.1.10/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M= diff --git a/process.go b/process.go new file mode 100644 index 0000000..cfc3dcf --- /dev/null +++ b/process.go @@ -0,0 +1,31 @@ +package stat + +import ( + "os" + "runtime" + "syscall" + + "kumoly.io/lib/klog" +) + +func IsRunning(pid int) bool { + p, err := os.FindProcess(pid) + if err != nil { + klog.Debug(err) + return false + } + if runtime.GOOS == "windows" { + return true + } + err = p.Signal(syscall.Signal(0)) + if err == syscall.ESRCH { + return false + } + // running + // if err == nil { + // return true + // } + + // other errors ignored but we know that the proccess is running + return true +} diff --git a/process_test.go b/process_test.go new file mode 100644 index 0000000..133eb68 --- /dev/null +++ b/process_test.go @@ -0,0 +1,10 @@ +package stat + +import ( + "fmt" + "testing" +) + +func TestIsRunning(t *testing.T) { + fmt.Println(IsRunning(26340)) +}