Link to home
Start Free TrialLog in
Avatar of Prabha S
Prabha S

asked on

inotifywait not listening new file creation in a directory

I am running inotifywait to listen new files on a directory. The directory has 600 permission and user is vmail.
and I am running inotifywait using root user.

this is the command I used

inotifywait -mq -e CREATE --format %w%f /var/vmail/xxx/unsubscribe/cur | while read FILE; do echo "$FILE"; done

Whenever I receive mail it's not identifying the new file.
When I manually create file using root user it's displaying the file name.

What I observed is when I receive mail, the permissions of the file is 600.
When I create the file using root the file permission is 622.

I tried to switch user from root to vmail and run the command but no use.

I am stuck here.

I need to unsubscribe the user whenever I receive new unsubscribe mail immediately.
ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Prabha S
Prabha S

ASKER

I added the -r option but didn't display the new file name when I received the new mail.
But when I create file using touch /var/vmail/xxx/unsubscribe/cur/temp in the directory I am seeing the output on the terminal.
I am looking at wrong directory.
Initially it was creating files under cur now its creating under new .
Now I added both directories to the inotifywait.

Now it's working. With or without -r option it's displaying content.
Correct.

With IMAP protocol (tmp/new/cur) there's a transition of file activity managed by the server, like Dovecot.

1) File is written into tmp via IMAP server.

2) Once the file is finished (CLOSE_WRITE via inotifywait), then the file is linked to new + deleted from tmp.

3) Now at some point in the future the IMAP client (mail reader) connects to Dovecot + says, give me a list
    of updated files. At this point, files in new move to cur via link/remove operation pair.

Likely best to camp on the parent of tmp/new/cur keeping in mind you won't really see a CREATE event many times.

As files progress through their life cycle you'll see events related to link/remove pairs.
Thanks for the update I will update the code accordingly.