-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocutil.go
More file actions
36 lines (30 loc) · 985 Bytes
/
Copy pathprocutil.go
File metadata and controls
36 lines (30 loc) · 985 Bytes
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
34
35
36
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package procutil
import (
"github.com/shirou/gopsutil/v4/process"
)
// IsProcessRunning checks if a process with the given PID is running.
// Works cross-platform (Windows, Linux, macOS, FreeBSD, OpenBSD, Solaris, AIX).
//
// This function uses github.com/shirou/gopsutil for reliable cross-platform
// process detection, including proper handling of stale PIDs on Windows.
func IsProcessRunning(pid int) bool {
if pid <= 0 {
return false
}
// Create a process handle
proc, err := process.NewProcess(int32(pid)) //nolint:gosec // G115: safe conversion, PIDs fit in int32
if err != nil {
// Process doesn't exist or can't be accessed
return false
}
// Check if process is running
// gopsutil handles platform differences correctly
isRunning, err := proc.IsRunning()
if err != nil {
// Error checking status, assume not running
return false
}
return isRunning
}