简体   繁体   English

Linux:如何检查我的监视文件夹中的文件副本是否已完成?

[英]Linux : How to check a file copy has finished in my watch folder?

I have a folder in which the different users can place their documents like pdf,tif etc. I go to check this folder every minute if there is any new file, if yes then i pick that file for processing.我有一个文件夹,不同的用户可以在其中放置他们的文档,如 pdf、tif 等。如果有任何新文件,我每分钟都会检查这个文件夹,如果有,那么我选择该文件进行处理。

But, I don't want to pick this file in case it is still being copied.但是,我不想选择这个文件,以防它仍在被复制。 In that case i will pick it in future cycle.在这种情况下,我会在未来的周期中选择它。

But the problem i'm facing is.但我面临的问题是。 That How can i check whether the file copy has been finished in my folder?那如何检查我的文件夹中的文件复制是否已完成?

I've already tried the following things.我已经尝试过以下事情。 1) Tried to open the file in Write mode using C program. 1) 尝试使用 C 程序以写入模式打开文件。 But I'm able to open it in write mode even if it is still being copied.但是即使它仍在被复制,我也可以以写入模式打开它。

2) Used lsof command to check if some process is associated with this. 2) 使用 lsof 命令来检查某个进程是否与此相关联。 But this command doesn't give results in case of non root user.但是这个命令在非 root 用户的情况下不会给出结果。

NOTE : I could be non root user on Linux.注意:我可能是 Linux 上的非 root 用户。

You can use kernel's inotify facility to listen for IN_CLOSE_WRITE event.您可以使用内核的inotify工具来监听 IN_CLOSE_WRITE 事件。 When copy is finished, program close file handle, which in turn triggers this events.复制完成后,程序关闭文件句柄,进而触发此事件。

IN_CLOSE_WRITE (+) IN_CLOSE_WRITE (+)
File opened for writing was closed.为写入而打开的文件已关闭。

There are couple of examples how to use it from shell, for example here有几个示例如何从 shell 中使用它,例如这里

Example for using with C here 在此处与 C 一起使用的示例

In most cases it is sufficient to assume that the copy has been finished, if the file has not been modified for some time.在大多数情况下,如果文件有一段时间没有被修改,假设副本已经完成就足够了。 Process just files not been modified the last 15 minutes:仅处理过去 15 分钟未修改的文件:

find . -mmin +15

A correct solution will require all programs to honor some kind of locking:正确的解决方案将要求所有程序都遵守某种锁定:

https://linux.die.net/man/2/flock https://linux.die.net/man/2/flock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM