Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

is there a way to provide post-mount and pre-umount scripts in Linux?
I am trying to do some scripts on storage media when mounted and before umounting (eg, for synching).

Any help appreciated.

share|improve this question

migrated from serverfault.com Mar 28 '10 at 11:01

This question came from our site for system and network administrators.

How is the filesystem being mounted? Things are not mounted automatically presumably you have some program/tool that is performing the mount for you. You need to determine what that is and replace it or fix it. –  Zoredache Mar 27 '10 at 21:39
In my case I normally use the KDE subsystem (presumably that is Dolphin, don't know which component is responsible for the actual mount command) –  Homer J. Simpson Mar 27 '10 at 21:45
i wrote about handling automounts with uDev or with HALevt in answers to another question. those would be replacements for the KDE or GVFS subsystem, but the procedure might give you some ideas of where to look. it sounds like you want to tap into the Dbus mounts (desktop/userspace rather than kernel level). –  quack quixote May 5 '10 at 6:33

4 Answers 4

you could write a udev rule to do that. I have used something like this for automatically backing up stuff to a usb drive. Fun stuff.

share|improve this answer
udev seems to not have an event for mount/unmount though. It fires just when you connect/disconnect a device. And your link doesn't work.. –  Pithikos Dec 1 '14 at 11:55
that was answered 4 years ago. I am not responsible for that site. But you could take a look at archive.org ..., yes, it's still there, finding it will be left as an exercise for the reader. As to your comment about udev, udev lets you write rules and execute actions when those are matched. It's all you need. –  natxo asenjo Dec 1 '14 at 18:09
Or you can just remove it since it's totally outdated :) –  Pithikos Dec 1 '14 at 18:15
why should I remove anything about something I posted 4 years ago and is easily found through archive.org? If you feel so compelled to do it, just get enough reputation points to edit stuff and do it yourself. I can not be bothered to be honest. As to it being outdated, well, that is your opinion and I suppose you know what people say about opinions ;-) –  natxo asenjo Dec 1 '14 at 19:33
the point is to help other people stumbling on the issue, it's not about points or other ego-boosts. Now even if a user goes through all that trouble, the solution there is outdated. So you are wasting people's time for no benefit (except the 1-2 up-votes you have). –  Pithikos Dec 2 '14 at 11:51

You can probably use the inotify kernel hook, although a pre-unmount event may be really tricky to catch as I'm not sure if there is an inotify event sent before the directory is umounted.

share|improve this answer

If you are trying to do this for people invoking the /sbin/mount and /sbin/umount command, all you'd have to do is make a wrapper program for both and program what you want into these wrapper programs. A wrapper program being replace the /sbin/mount with the wrapper program that calls the real /sbin/mount (renamed to something like /sbin/mount-real or /sbin/umount-real). To prevent anybody from calling the real program, use group permissions to restrict it and set the SGID bit on the wrapper programs.

If you are looking to fix the mount(2) library call, well that is an effort and probably difficult and probably not advisable as this could cause problems for internal systems processing especially during startup and shutdown.

Even with a wrapper program you'd probably have to throughly test it to avoid system interference and made appropriate adjustments.

share|improve this answer

The simplest solution that is probably going to work forever is to poll the /etc/mtab or /proc/mounts. Once a change to that file is made, it means that either something got mounted or unmounted. Then you have to check there for the device you want.

Udev is not the best solution since it just tells you if a device connected. It doesn't say if the device has actually mounted or not. This might work sometimes but it's a hacky solution that at some point will break and you will be pulling your hair out.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.