feat: getPidByPort() supports alpine now (#3483)

Signed-off-by: WindSpiritSR <simon343riley@gmail.com>
This commit is contained in:
WindSpiritSR 2025-01-08 12:18:46 +08:00 committed by GitHub
parent 3feb6ce84d
commit c5f25cbc7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -29,7 +29,7 @@ func getPidByPort(port int) (int, error) {
case "windows": case "windows":
cmd = exec.Command("cmd", "/c", "netstat -ano | findstr :"+strconv.Itoa(port)) cmd = exec.Command("cmd", "/c", "netstat -ano | findstr :"+strconv.Itoa(port))
case "darwin", "linux": case "darwin", "linux":
cmd = exec.Command("lsof", "-i", ":"+strconv.Itoa(port)) cmd = exec.Command("lsof", "-t", "-i", ":"+strconv.Itoa(port))
default: default:
return 0, fmt.Errorf("unsupported OS: %s", runtime.GOOS) return 0, fmt.Errorf("unsupported OS: %s", runtime.GOOS)
} }
@ -59,7 +59,7 @@ func getPidByPort(port int) (int, error) {
return pid, nil return pid, nil
} }
} else { } else {
pid, err := strconv.Atoi(fields[1]) pid, err := strconv.Atoi(fields[0])
if err != nil { if err != nil {
return 0, err return 0, err
} }