Locking Files in Bash

痴心易碎 提交于 2019-12-20 02:27:28

问题


I have a Problem to find a good concept on locking files in bash,

Basically I want to achieve the following:

  1. Lock File
  2. Read in the data in the file (multiple times)
  3. Do stuff with the data.
  4. Write new stuff to the file (not necessarily to the end)
  5. Unlock that file

Doing this with flock seems not possible to me, because the file descriptor will just move once to the end of the file.

Also creating a Tempfile fails, because I might overwrite already read lines which is also not possible.

Edit:
Also note that other scripts I do not control might try to write to that file.

So my question is how can I create a lock in step 1 so it will span over steps 2,3,4 till I unlock it again in step 5?


回答1:


You can do this with the flock utility. You just need to get flock to use a separate read-only file descriptor, i.e. open the file twice. E.g. to sort a file using a intermediate temporary file:

(
    flock -x -w 10 100 || exit 1

    tmp=$(mktemp)

    sort <"$file" >"$tmp"

    cat "$tmp" > "$file"

    rm -f "$tmp"
) 100<"$file"

flock will issue the flock() system call for your file and block if it is already locked. If the timeout is exceeded then the script will just abort with an error code.



来源:https://stackoverflow.com/questions/22506857/locking-files-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!