Linux flock,如何锁定 "just"文件?

标签 linux bash file-locking flock

在 Bash 中,我试图使函数 getLock 与不同的锁名称一起使用。

function getLock
{
    getLock_FILE="${1}"
    getLock_OP="${2}"
    case "${getLock_OP}" in
        "LOCK_UN")
            flock -u "${getLock_FILE}"
            rm -fr "${getLock_FILE}"
            ;;
        "LOCK_EX")
            flock -x "${getLock_FILE}"
    esac
}

但是 flock 说 flock: bad number: myfilelock

我怎样才能只锁定一个文件,然后在我想要的时候释放它,而不必在 flock 中执行命令?

它是这样使用的:

getLock myfilelock LOCK_EX
somecommands
........
getLock myfilelock LOCK_UN

最佳答案

锁定文件:

exec 3>filename # open a file handle; this part will always succeed
flock -x 3      # lock the file handle; this part will block

释放锁:

exec 3>&-       # close the file handle

您也可以按照 flock 手册页描述的方式进行操作:

{
  flock -x 3
  ...other stuff here...
} 3>filename

...在这种情况下,文件会在 block 退出时自动关闭。 (这里也可以使用子 shell,通过使用 ( ) 而不是 { } ,但这应该是一个深思熟虑的决定——因为子 shell 有性能损失,并且范围变量修改和其他状态更改自身)。


如果您运行的是足够新的 bash 版本,则无需手动管理文件描述符编号:

# this requires a very new bash -- 4.2 or so.
exec {lock_fd}>filename  # open filename, store FD number in lock_fd
flock -x "$lock_fd"      # pass that FD number to flock
exec $lock_fd>&-         # later: release the lock

...现在,对于您的功能,我们将需要关联数组和自动 FD 分配(并且,为了允许从不同路径锁定和解锁同一文件,GNU readlink)——所以这不会不适用于较旧的 bash 版本:

declare -A lock_fds=()                        # store FDs in an associative array
getLock() {
  local file=$(readlink -f "$1")              # declare locals; canonicalize name
  local op=$2
  case $op in
    LOCK_UN)
      [[ ${lock_fds[$file]} ]] || return      # if not locked, do nothing
      exec {lock_fds[$file]}>&-              # close the FD, releasing the lock
      unset lock_fds[$file]                   # ...and clear the map entry.
      ;;
    LOCK_EX)
      [[ ${lock_fds[$file]} ]] && return      # if already locked, do nothing
      local new_lock_fd                       # don't leak this variable
      exec {new_lock_fd}>"$file"              # open the file...
      flock -x "$new_lock_fd"                 # ...lock the fd...
      lock_fds[$file]=$new_lock_fd            # ...and store the locked FD.
      ;;
  esac
}

如果您在 GNU readlink 不可用的平台上,我建议将 readlink -f 调用替换为来自 sh-realpath by Michael Kropatrealpath (仅依赖于广泛可用的阅读链接功能,而不是 GNU 扩展)。

关于Linux flock,如何锁定 "just"文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24388009/

相关文章:

linux - 通过 SSH 运行脚本是可行的。但是通过 xinetd 或切换用户后它将无法工作

linux - 查找包含 20 个元音字母的单词 grep

json - 将 JSON 转换为 bash 环境变量

bash - 使用 Hadoop : localhost: Error: JAVA_HOME is not set

linux - nfsv4 linux c 中的并发文件访问

java - 截断锁定文件

c - 求一个内核宏: hlist_nulls_for_each_entry的解释

linux - 如何在 Pymol 中改变角度?

bash - 检查进程是否正在运行,如果已经运行则退出