Mikhail T.
Mikhail T.

Reputation: 3977

Watchdog module does not notify of directory rename?

I need to monitor files, which are sometimes created by an unwieldy application in sub-subdirectories of a logs/ directory:

logs/foo/per_process_dir/journal.log

My little daemon is written in Python and uses the watchdog-module to watch over the files (inotify is used by the module on Linux). I simply ask the module to monitor the foo/ subdirectory (recursively) and it notifies me, whenever a journal is appended...

This all works but... The whole logs/ directory is rotated, when the application is restarted -- and I'd like my daemon to notice this automatically so that there'd be no need to restart it too.

I expected to receive a "moved" event -- when logs/ is renamed to logs-Sunday/, for example -- but it does not happen... The daemon is small currently and I loath having to enlarge it by adding the code for watching the logs/ folder separately.

Is there some other way, perhaps?

Upvotes: 1

Views: 492

Answers (1)

larsks
larsks

Reputation: 311576

If you want to catch renames of the logs directory, you will need to attach your observer to its parent directory. That is, if your logs directory is actually appname/logs, then instead of calling, e.g.:

observer.schedule(event_handler, 'appname/logs', recursive=True)

You would use:

observer.schedule(event_handler, 'appname', recursive=True)

(And you will subsequently need to filter events and ignore those that are outside of the logs directory.)


This happens because your filesystem observer is attached to the logs directory. When you rename the logs directory, your observer continues to monitor it...under the new name. That is, the observer is attached to the inode, not to the path.

Upvotes: 1

Related Questions