如何知道用户当前打开了哪些文件?

发布于 2024-11-10 09:53:54 字数 221 浏览 6 评论 0原文

我正在尝试编写一个脚本或一段代码来归档文件,但我不想归档当前打开的任何内容。我需要找到一种方法来确定目录中的哪些文件已打开。我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岛歌少女 2024-11-17 09:53:55

您处理问题的方式不正确。您希望在阅读时防止文件在您的下面被修改,并且如果没有操作系统支持就无法做到这一点。在多用户系统中,您所能期望的最好的结果就是保持存档元数据的一致性。

例如,如果您要创建存档目录,请确保存档中存储的字节数与该目录匹配。您可以在读取文件系统之前和之后对文件内容进行校验和,并将其与写入存档的内容进行比较,并可能将其标记为“不一致”。

你想实现什么目标?

针对评论添加:

查看logrotate以窃取有关如何一致处理此问题的想法,只需让它为您完成工作即可。如果您担心文件重命名会使当前正在写入文件的进程破坏某些内容,请查看 man 2 rename

rename() 重命名文件并移动它
如果需要的话,在目录之间。任何
到该文件的其他硬链接(如
使用 link(2) 创建的)不受影响。
oldpath 的打开文件描述符是
也不受影响。

如果 newpath 已经存在,它将被自动替换(主题
满足几个条件;查看错误
下面),这样就没有意义了
另一个进程试图
访问newpath会发现缺少了。

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 at man 2 rename:

rename() renames a file, moving it
between directories if required. Any
other hard links to the file (as
created using link(2)) are unaffected.
Open file descriptors for oldpath are
also unaffected.

If newpath already exists it will be atomically replaced (subject
to a few conditions; see ERRORS
below), so that there is no point at
which another process attempting to
access newpath will find it missing.

玩物 2024-11-17 09:53:55

以 root 身份尝试 ls -l /proc/*/fd/*。

Try ls -l /proc/*/fd/* as root.

时光瘦了 2024-11-17 09:53:55

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文