创建锁文件
lockFile := "./lock.pid"
lock, err := os.Create(lockFile)
if err != nil {
log.Fatal("创建文件锁失败", err)
}
defer os.Remove(lockFile)
defer lock.Close()
lockFile
锁定文件
err = syscall.Flock(int(lock.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err != nil {
log.Println("上一个任务未执行完成,暂停执行")
os.Exit(1)
}
syscall.LOCK_EXsyscall.LOCK_NBlock.Fd()
defer syscall.Flock(int(lock.Fd()), syscall.LOCK_UN)