如何知道用户当前打开了哪些文件?
我正在尝试编写一个脚本或一段代码来归档文件,但我不想归档当前打开的任何内容。我需要找到一种方法来确定目录中的哪些文件已打开。我想使用 Perl 或 shell 脚本,但如果需要的话可以尝试使用其他语言。它将在 Linux 环境中,我无法选择使用 lsof。我的定影器也得到了不一致的结果。感谢您的任何帮助。
我正在尝试获取目录中的日志文件并将它们移动到另一个目录。但是,如果文件已打开,我不想对它们执行任何操作。
I am trying to write a script or a piece of code to archive files, but I do not want to archive anything that is currently open. I need to find a way to determine what files in a directory are open. I want to use either Perl or a shell script, but can try use other languages if needed. It will be in a Linux environment and I do not have the option to use lsof. I have also had inconsistant results with fuser. Thanks for any help.
I am trying to take log files in a directory and move them to another directory. If the files are open however, I do not want to do anything with them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您处理问题的方式不正确。您希望在阅读时防止文件在您的下面被修改,并且如果没有操作系统支持就无法做到这一点。在多用户系统中,您所能期望的最好的结果就是保持存档元数据的一致性。
例如,如果您要创建存档目录,请确保存档中存储的字节数与该目录匹配。您可以在读取文件系统之前和之后对文件内容进行校验和,并将其与写入存档的内容进行比较,并可能将其标记为“不一致”。
你想实现什么目标?
针对评论添加:
查看
logrotate
以窃取有关如何一致处理此问题的想法,只需让它为您完成工作即可。如果您担心文件重命名会使当前正在写入文件的进程破坏某些内容,请查看man 2 rename
:You are approaching the problem incorrectly. You wish to keep files from being modified underneath you while you are reading, and cannot do that without operating system support. The best that you can hope for in a multi-user system is to keep your archive metadata consistent.
For example, if you are creating the archive directory, make sure that the number of bytes stored in the archive matches the directory. You can checksum the file contents before and after reading the filesystem and compare that with what you wrote to the archive and perhaps flag it as "inconsistent".
What are you trying to accomplish?
Added in response to comment:
Look at
logrotate
to steal ideas about how to handle this consistently just have it do the work for you. If you are concerned that rename of files will make processes that are currently writing them will break things, take a look atman 2 rename
:以 root 身份尝试 ls -l /proc/*/fd/*。
Try
ls -l /proc/*/fd/*
as root.msw 已正确回答了问题,但如果您想归档打开进程的列表,则 lsof 命令会把它给你。
msw has answered the question correctly but if you want to file the list of open processes, the lsof command will give it to you.