Linux-Noob Forums

Full Version: how do I run a script whenever a device is mounted?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

How do I run a script whenever a device is mounted?


The device could be a partition of an internal disk already connected at startup, or an external usb drive to be plugged in.


I found a solution using a udev rule but it's working only for usb drive so far.


Any suggestion for the internal partition issue instead?


Many thanks to all

Not sure about internal partitions - these usually get mounted at boot-time (from fstab) so I'm guessing some script running early in the startup framework (if using /etc/init.d/ framework) that checks if the desired partition is mounted then triggers some action would be the way I'd imagine.


USB stuff tends to be handled by the event-driven framework (/etc/event.d/) now, since these aren't always present during bootup and often want to be mounted/unmounted during the course of normal operations, hence the udev idea. I imagine some event-trapping routine could be modified to fire off when a specific partition is mounted, but as to how that's done... out of my league, sorry!


An alternative approach is to have some security (auditing) utilities watch logfiles for reports of the partition being successfully mounted then fire trigger an action accordingly - the only issue there is having the partitions containing logfiles and utils mounted early (and util running) before other slices come online.


What is it you're trying to achieve? Perhaps there could be another route.