I tried the following code and I am not getting any notification when mounting and unmounting partitions. Am I missing something ?
I use the library pyinotify
to watch the changes in the file /proc/mounts
… but it seems that my handler is never called…
import pyinotify, time
# Define a callback function to handle file system events
class EventHandler(pyinotify.ProcessEvent):
def process_IN_MODIFY(self, event):
print(f"Change detected in {event.pathname}!")
# Create an inotify instance
wm = pyinotify.WatchManager()
# Create an event handler instance
handler = EventHandler()
# Create an inotify notifier and pass the function as the event handler
notifier = pyinotify.ThreadedNotifier(wm, handler)
# Watch for changes to /proc/mounts
wm.add_watch("/proc/mounts", pyinotify.IN_MODIFY)
# Start the notifier loop
print("Watching for mount changes...")
notifier.start()
try:
while True:
time.sleep(1)
except: pass
notifier.stop()
print('Done!')