Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 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

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
inotify will tell you that a directory has been created (e.g. under /media or /mnt),but not if a filesystem has been mounted on that directory. – Craig McQueen Jul 30 '15 at 1:41

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
"hacky" is usually good enough for most situations. The real world is run by "hacky" solutions. If you do not know that then you have no real world experience using computer systems. Please save your opinions to yourself and stick to the facts. Thanks. – natxo asenjo Aug 3 '15 at 6:46
@natxoasenjo that's why there are good developers and bad developers. – Pithikos Aug 3 '15 at 10:07
please come back when you have some real world experience. What you propose is to write some script/program to poll files and you think that is a better idea than to use a well tested api as udev. Who is the bad developper? ;-) – natxo asenjo Aug 3 '15 at 14:03
and by the way, writing udev rules does not make you a developer, at most a sysadmin of your computer. – natxo asenjo Aug 3 '15 at 14:12
@natxoasenjo well if you don't see that the "real world" programs break all the time due to hackish solutions then either you are blind or don't have "real world experience". Udev works fine but has nothing to do with mounting and unmounting. And please stop turning this into a forum thread about who is a programmer. Doesn't help anyone. – Pithikos Aug 4 '15 at 17:12

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 ..., 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 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

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.