简体   繁体   中英

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.

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. 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. But this command doesn't give results in case of non root user.

NOTE : I could be non root user on Linux.

You can use kernel's inotify facility to listen for IN_CLOSE_WRITE event. When copy is finished, program close file handle, which in turn triggers this events.

IN_CLOSE_WRITE (+)
File opened for writing was closed.

There are couple of examples how to use it from shell, for example here

Example for using with C here

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:

find . -mmin +15

A correct solution will require all programs to honor some kind of locking:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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