Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

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

I have written a tool which automatically formats and copies files to USB flash drives when they are inserted. It needs the drive to be unmounted to format it.

Normally Nautilus will automatically mount the drive when inserted, so I have to wait for this to happen and then unmount the drive before formatting it. The only problem is that if the user has disabled automounting then my program will wait forever.

I could change the dconf auto-mount setting when my program starts, and then change it back when it exits, but this is not a good solution because the setting wil not be restored if my program crashes.

How can I temporarily prevent Nautilus from automounting USB drives only during the time when my program is running?

share|improve this question
This might be a duplicate question of (…) – thom Jul 24 '13 at 16:11

You may use PROGRAM to check if your program is running to activate that UDEV rule, otherwise it is disabled because pgrep returns unsuccessfully.


Execute a program to determine whether there is a match; the key is true if the program returns successfully. The device properties are made available to the executed program in the environment. The program's standard output is available in the RESULT key.

This can only be used for very short-running foreground tasks. For details, see RUN.

source: man udev

  1. Add new rules file


    ACTION=="add", KERNEL="sd[a-z][0-9]*", DRIVERS=="usb-storage", PROGRAM="/usr/bin/pgrep yourprogramname", ENV{UDISKS_IGNORE}="1"
  2. Reload rules

    sudo control --reload-rules

BTW, udisk2 is the one responsible for auto-mounting

share|improve this answer

UPDATE: I have ignored the most important specification: "without making permanent changes to the system". Still solvable I'd say, but not without some deep research work into desktop session life cycle or desktop event handling. That ain't my expertise. ;)

Obviously you know already how to switch off the feature in question. If you're concerned that it is not switched back if your program crashes then there are only two possibilities: Take care that your program does not crash or program a program that watches your program and switches the feature back when your program is not running.

But maybe there is a different solution. You could append a line like this to /etc/fstab:

LABEL=somemagic /path ntfs noauto,user 0 0

Then if the filesystem on the drive carries that label it remains untouched by the automount feature and you can (u)mount it without disturbance. See man mount and man fstab for details. You could also use UUID instead of LABEL, but that depends on your use case and filesystem.

share|improve this answer
That doesn't really help unfortunately. In the case where the computer crashes it does not matter how many watchdog programs are running. Changing fstab is a permanent change, and I don't know the label of the drive before I plug it in. – Alistair Buxton Aug 26 '13 at 12:53
Restoring the setting at the next run is not an option because the user might never decide to run my program again. – Alistair Buxton Aug 26 '13 at 12:59
How will the state be restored? – Alistair Buxton Aug 26 '13 at 13:01
That was a rhetorical question: the state won't be restored unless the user runs some piece of software to restore it, which they might not do. – Alistair Buxton Aug 26 '13 at 13:03
let us continue this discussion in chat – Johannes Aug 26 '13 at 13:10

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.