Is there a particular reason you are using:
| awk '($2=="CREATE"){print $3}' > ~/output.file
instead inotifywait
options like --format
and --outfile
?
If I run:
inotifywait -m --format '%f' -e create /home/don/folder/ --outfile /home/don/output.file
then open another tab, cd
to ~/folder
and run:
time seq -w 00001 50000 | parallel touch {}
real 1m44.841s
user 3m22.042s
sys 1m34.001s
(so I get much more than 500 files per minute) everything works fine and output.file
contains all the 50000
file names that I just created.
Once the process has finished writing the files to disk you can append them to your test.out
(assuming you are always in ~/folder
):
xargs < /home/don/output.file cat >> final.file
Or use read
if you want to process files as they are created. So, while in ~/folder
you could run:
inotifywait -m --format '%f' -e create ~/folder | while read file; do cat -- "$file" >> ~/test.out; done
Note that in inotifywait
stable, -m
and -t
cannot be used together. Support for usage of both switches has been recently added so if you build inotify-tools
from git
you should be able to use monitor
with timeout
(to specify how long it has to wait for an appropriate event to occur before exiting). I've tested the git
version on my system (exit if no create
events occur within 2 seconds) and it works fine:
inotifywait -m -t 2 --format '%f' -e create ~/folder | while read file; do cat -- "$file" >> ~/test.out; done
When inotifywait
outputs the string "Watches established." is secure to make changes in the watched inodes, so you should wait to the string to appears on standard error before touching the files.
As an example,this code should to that,
inotifywait -r -m -e close_write "somedir" \
2> >(while :;do read f; [ "$f" == "Watches established." ] && break;done;\
find "somedir" -type f -exec touch {} ";")\
| while read f; do echo "$f hi";done
Best Answer
You can observe which events are passed to your directory with
inotifywait
to check how does it behave when your process is running:For example, after running
touch file
in same directory:Since there is delay between creating file and closing it, you'll have empty file after CREATE event, as you suspected. Observe events coming after running:
Therefore using CLOSE_WRITE event sounds reasonable.