File detection is closed

Is there an easy way to detect when a file is closed for writing? I have a process that gives out a new file every 5 seconds. I cannot change the code for this process. I am writing another process to parse the contents of each file immediately after it is created. Is there an easy way to view a specific directory to see when the file is no longer open for writing? I am hoping for some Linux command.

+2


a source to share


5 answers


you can put something on top of strace to raise your events.

try:



strace ls 2>&1 | grep "close\|open"

      

Using inotify is also a good idea, but it will probably take more coding to get what you want.

+2


a source


If you know that there is some text (line) in the file indicating the end of the write, you can easily monitor it using commands like grep



Alternatively, you can use the command fuser

to see if a file is in use.

+2


a source


Since you are using Linux as your platform, you may need to look at the inotify system calls. When tracing a directory the output files were written, you will get open / closed events for the files created there.

If you want to integrate this into a shell script take a look at inotifywait which is part of the inotify tools.

+2


a source


You can lock the contents of your file until you are finished using it.

+1


a source


If you know the process ID of the process that writes the files, you can look /proc/<pid>/fd/

to see which files were open.

+1


a source







All Articles