io方向操作会锁定文件吗?
will io direction operation lock the file?
我已经有一个 growing nginx 日志文件 20G,我想轮换它。
1,i mv
旧日志文件到新日志文件
2,我> old_log_file.log
在大约2~3秒内截断旧日志文件
当我进行截断(大约 2~3 秒)时,旧日志文件是否有锁(写锁?)?
在那 2~3 秒期间,nginx returns 502 等待将日志附加到旧日志文件直到锁定释放?
谢谢你的解释。
是否锁定文件取决于应用程序。生成此日志文件的应用程序必须具有清除日志文件的选项。一个示例是在编辑器中,例如 vim 文件在编辑器中仍处于打开状态时可以对其进行外部修改。
在 Linux 上,(几乎)没有强制性的 file locks (more precisely, there used to be some mandatory locking feature in the kernel, but it is deprecated and you really should avoid using it). File locking happens with flock(2) or lockf(3) and is advisory and should be explicit (e.g. with flock(1) 命令,或者某些程序调用 flock
或 lockf
)。
所以每个与文件相关的锁定实际上是所有使用该文件的软件之间的约定(以及mv(1)或您的shell重定向'不使用文件锁定)。
请记住 Linux 上的文件主要是 i-node (see inode(7)) which could have zero, one or several file paths (see path_resolution(7) and be aware of link(2), rename(2), unlink(2)) and used thru some file descriptor. Read ALP (and perhaps Operating Systems: Three Easy Pieces) 更多内容。
在你的问题的场景中没有发生文件锁定(并且涉及的i节点和文件描述符是独立的)。
考虑使用 logrotate(8)。
一些软件提供了一种重新加载其配置和重新打开日志文件的方法。您应该阅读 nginx.
的文档
我已经有一个 growing nginx 日志文件 20G,我想轮换它。
1,i mv
旧日志文件到新日志文件
2,我> old_log_file.log
在大约2~3秒内截断旧日志文件
当我进行截断(大约 2~3 秒)时,旧日志文件是否有锁(写锁?)?
在那 2~3 秒期间,nginx returns 502 等待将日志附加到旧日志文件直到锁定释放?
谢谢你的解释。
是否锁定文件取决于应用程序。生成此日志文件的应用程序必须具有清除日志文件的选项。一个示例是在编辑器中,例如 vim 文件在编辑器中仍处于打开状态时可以对其进行外部修改。
在 Linux 上,(几乎)没有强制性的 file locks (more precisely, there used to be some mandatory locking feature in the kernel, but it is deprecated and you really should avoid using it). File locking happens with flock(2) or lockf(3) and is advisory and should be explicit (e.g. with flock(1) 命令,或者某些程序调用 flock
或 lockf
)。
所以每个与文件相关的锁定实际上是所有使用该文件的软件之间的约定(以及mv(1)或您的shell重定向'不使用文件锁定)。
请记住 Linux 上的文件主要是 i-node (see inode(7)) which could have zero, one or several file paths (see path_resolution(7) and be aware of link(2), rename(2), unlink(2)) and used thru some file descriptor. Read ALP (and perhaps Operating Systems: Three Easy Pieces) 更多内容。
在你的问题的场景中没有发生文件锁定(并且涉及的i节点和文件描述符是独立的)。
考虑使用 logrotate(8)。
一些软件提供了一种重新加载其配置和重新打开日志文件的方法。您应该阅读 nginx.
的文档