mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-07 07:10:27 +08:00
38 lines
661 B
Go
38 lines
661 B
Go
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
|
|
package utils
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
ProcDir = "/proc"
|
|
)
|
|
|
|
func FindPidWithName(name string) int {
|
|
// process name
|
|
commFiles, err := filepath.Glob(ProcDir + "/*/comm")
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
|
|
for _, commFile := range commFiles {
|
|
data, err := os.ReadFile(commFile)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if strings.TrimSpace(string(data)) == name {
|
|
var pieces = strings.Split(commFile, "/")
|
|
var pid = pieces[len(pieces)-2]
|
|
pidInt, _ := strconv.Atoi(pid)
|
|
return pidInt
|
|
}
|
|
}
|
|
|
|
return 0
|
|
}
|