glogcenter/glc/onstart/pid.go
gotoeasy da22db315b pid
2022-09-14 10:10:22 +08:00

68 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package onstart
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
type PidFile struct {
Path string // pid文件路径
Pid string // pid
IsNew bool // 是否新
Err error // error
}
// 指定路径下生成pid文件文件内容为pid已存在时检查pid有效性
func NewPid(pathfile string) *PidFile {
// 运行中时直接返回
if opid := checkPidFile(pathfile); opid != nil {
return opid
}
// 创建文件
if err := os.MkdirAll(filepath.Dir(pathfile), os.FileMode(0755)); err != nil {
log.Println("create pid file failed", pathfile)
return &PidFile{
Path: pathfile,
Err: err,
}
}
// 保存PID
pid := fmt.Sprintf("%d", os.Getpid())
if err := os.WriteFile(pathfile, []byte(pid), 0644); err != nil {
log.Println("save pid file failed", pathfile)
return &PidFile{
Path: pathfile,
Err: err,
}
}
// 成功创建后返回
return &PidFile{
Path: pathfile,
Pid: pid,
IsNew: false,
Err: nil,
}
}
func checkPidFile(path string) *PidFile {
if pidByte, err := os.ReadFile(path); err == nil {
pid := strings.TrimSpace(string(pidByte))
if _, err := os.Stat(filepath.Join("/proc", pid)); err == nil {
return &PidFile{
Path: path,
Pid: pid,
IsNew: false,
}
}
}
return nil
}