sk-matrix-project/app/wails/pkg/pid/pid_unix.go

37 lines
719 B
Go
Raw Normal View History

2023-07-12 00:35:32 +08:00
//go:build !windows
package pid
import (
"os"
"skapp/pkg/logger"
"syscall"
)
func (p *PidLock) Lock() error {
_, err := os.Stat(p.file)
if err == nil {
os.Exit(0)
}
pidFileHandle, err := os.OpenFile(pidFile, os.O_RDONLY|os.O_CREATE, os.ModePerm)
if err != nil {
logger.Log.Fatalf("启动失败 进程锁文件 .pid 创建失败")
return LockFail
}
err = syscall.Flock(int(pidFileHandle.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err != nil {
logger.Log.Fatalf("启动失败 进程锁文件 .pid 被占用")
return LockFail
}
p.lock = pidFileHandle
return nil
}
func (p *PidLock) UnLock() {
_ = syscall.Flock(int(p.lock.Fd()), syscall.LOCK_UN)
_ = p.lock.Close()
_ = os.Remove(p.file)
}