gregghz / Watcher

Watcher is a daemon that watches specified files/folders for changes and fires commands in response to those changes. It is similar to incron, however, configuration uses a simpler to read yaml file instead of a plain text file. It's also written in Python, making it easier to hack.
MIT License
245 stars 128 forks source link

Notifications for .swpx, .swp, .lck files. Also, how to monitor a directory and its sub directories only (not files)? #18

Closed jiraz2112114 closed 9 years ago

jiraz2112114 commented 9 years ago

First of all, a great script. Very useful. Thanks for your work on this.

Everything is working as expected for me. The watcher notifies me about file deletions alright. But, it tends to notify me about .swp, .swpx (vim temp files) and .lck files, is there a way to exclude the notifications for these temp files. Also, while I am researching into it myself, was anyone able to monitor a directory and its sub directories only (not files) using the script?

For e.g.,

2015-02-13 20:11:23 /abc/xyz/.test.swpx was deleted 2015-02-13 20:11:24 /abc/xyz/.test.swp was deleted 2015-02-13 20:12:50 /abc/xyz/.test1234.swpx was deleted 2015-02-13 20:12:50 /abc/xyz/.test1234.swp was deleted 2015-02-13 20:12:56 /abc/xyz/.test1234.swp was deleted 2015-02-14 22:38:23 /abc/xyz/.stfs.lck was deleted 2015-02-14 22:38:28 /abc/xyz/xyz.pdf was deleted 2015-02-14 22:40:37 /abc/xyz/xyz.pdf was deleted

gregghz commented 9 years ago

The easiest way to handle both of these situations is with the script that's called when a job is triggered. Just use some basic bash stuff to not do anything on particular files or to only perform actions on directories. Watcher is meant to be a pretty basic tool that only barely expands on what inotify already does so I never got as far as blacklists or anything of that sort.

Let me know and I can help out with those has scripts.

Thanks,

Gregg

On Sun, Feb 15, 2015, 10:16 AM jiraz2112114 notifications@github.com wrote:

First of all, a great script. Very useful. Thanks for your work on this.

Everything is working as expected for me. The watcher notifies me about file deletions alright. But, it tends to notify me about .swp, .swpx (vim temp files) and .lck files, is there a way to exclude the notifications for these temp files. Also, while I am researching into it myself, was anyone able to monitor a directory and its sub directories only (not files) using the script?

— Reply to this email directly or view it on GitHub https://github.com/gregghz/Watcher/issues/18.

jiraz2112114 commented 9 years ago

Thanks, Gregg. That simple thing really didn't occur to me. I was busy fighting with the exclude option instead (was trying to use all kinds of wildcards and make it work :-)). I can use the shell script to filter out the temporary files like you mentioned above.

How do I go about the directory part? Any ideas? I only want to monitor a Directory and its sub directories, not files. All files may not have the same extensions to filter everything out based on names using the shell script.

I am getting close. Thanks again for your response.

gregghz commented 9 years ago

An easy way to check (though it won't work if you want it to follow symlinks) is to use ls -dl. d for show info on directories specifically instead of listing the contents and l to make it a long listing.

So with whatever is passed from watcher to your script (called $filename here), you can do something like this:

if [[ -n $(ls -ld $filename | grep '^d') ]]; then echo '$filename is a directory' else echo '$filename is not a directory' fi

So if the ls -ld starts with a d (grep '^d'), that means it's a directory. Otherwise it's not a directory. Though it could be a symlink to a directory. It gets more complicated if you want to match symlinks to directories too.

On Mon Feb 16 2015 at 6:04:00 PM jiraz2112114 notifications@github.com wrote:

Thanks, Gregg. That simple thing really didn't occur to me. I was busy fighting with the exclude option instead (was trying to use all kinds of wildcards and make it work :-)). I can use the shell script to filter out the temporary files I mentioned above.

How do I go about the directory part? Any ideas? I only want to monitor a Directory and its sub directories, not files. All files may not have the same extensions to filter everything out based on names using the shell script.

I am getting close. Thanks again for your response.

— Reply to this email directly or view it on GitHub https://github.com/gregghz/Watcher/issues/18#issuecomment-74601704.

jiraz2112114 commented 9 years ago

But since I am monitoring for deletions, by the time things get to my script from the watcher, the file/directory will be deleted already. The if statement won't work as $filename doesn't exist anymore on the filesystem.

gregghz commented 9 years ago

Ooooo. That's a good point. I hadn't considered that. This is a feature request then, I don't know of any way to solve this with the bash script. When I get some time, I can try looking into this.

In the mean time, whatever is removing the files could possibly write to another file at the same time taking not of which files were directories? I understand that this may not be an option though.

On Tue Feb 17 2015 at 8:11:23 AM jiraz2112114 notifications@github.com wrote:

But since I am monitoring for deletions, by the time things get to my script from the watcher, the file/directory will be deleted already. The if statement won't work as $filename doesn't exist anymore on the filesystem.

— Reply to this email directly or view it on GitHub https://github.com/gregghz/Watcher/issues/18#issuecomment-74682432.

jiraz2112114 commented 9 years ago

Yes. A feature add request. I am no Python whiz :-), but I will keep looking for a solution. If I do find something meanwhile, I will update it here. Thanks again for your work. I have done enough research to say that there is no other script/project that does so accurate filesystem event reporting watching a filesystem recursively.

jiraz2112114 commented 9 years ago

I was able to solve using inotifywait by compiling inotify-tools from source as non root. Note that inotifywait doesn't start watchers on its own for newly created sub-directories/files while watching a filesystem recursively. In order to make it do that I am watching my filesytem for both create and delete events. For each new create event, a script will restart both the delete and create watcher scripts. For a delete event, another script will check if it is a directory (inotifywait writes a keyword "ISDIR" to output if the event is related to directory) and then send a delete notification and restart the create watcher script.

jiraz2112114 commented 9 years ago

I wanted to use this project as there is no need to re-establish the watchers when new sub-directories get created while watching a filesystem recursively. I added < 'isdir': event.dir, > to your code within the block below.

command = t.safe_substitute({ 'watched': event.path, 'filename': event.pathname, 'dest_file': dfile, 'tflags': event.maskname, 'nflags': event.mask, 'src_path': src_path, 'src_rel_path': src_rel_path, 'isdir': event.dir, 'datetime': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') })

The output I am starting to get below. See the first entry of the log? The script started printing if the delete event is related to directory! That's all i need! The shell script can take care of the rest.

2015-03-02 15:47:08 /xyz/abc/test/test1234 IN_DELETE|IN_ISDIR True 2015-03-02 15:50:19 /xyz/abc/test/.test1234.swpx IN_DELETE False 2015-03-02 15:50:19 /xyz/abc/test/.test1234.swp IN_DELETE False 2015-03-02 15:50:23 /xyz/abc/test/.test1234.swp IN_DELETE False 2015-03-02 15:50:29 /xyz/abc/test/test1234 IN_DELETE False

Updating this for others' benefit.

gregghz commented 9 years ago

do you want to create a pull request for this? I'm sure someone else has/will come up with this issue.