I want my Ubuntu 15.04 x64 server system to execute a script whenever a certain flash drive is inserted. So I created a custom rule file with the following content


The script executed whenever the flash drive is inserted, but I have two problems with it:

1) The script is apparently not executed when the computer is (re-)started with the drive already inserted. Is there any way to execute the script after startup if the flash drive is already connected?

2) The script wants to mount the device, but If I try to mount the device like

mount LABEL=mylabel /some/path


mount UUID=my-uuid /some/path

The source cannot be found. Currently I inserted a

sleep 5s

into my script, but this does not seem like a reliable solution. Is there any (failure tolerant) way to wait for the system to be sufficiently initialized?

For testing purposes, the script only contained

date >> /tmp/test.log
  • The script is executed during startup if the drive is already present. But it's executed pretty early in the boot process, so maybe your script is failing because it needs something that isn't available yet. Is the script located on the root filesystem? Show us its contents. Commented Sep 27, 2015 at 20:33
  • @Gilles for testing purposes, the script only contained the command date >> /tmp/test.log, (in addition to the shebang) and if I rebooted the system while the pen drive was still inserted, no entry was made.
    – muffel
    Commented Sep 28, 2015 at 6:25
  • I'm suspect that /tmp is not yet mounted read-write at that point. At least on Debian jessie with sysvinit, / is still mounted read-only and filesystems from /etc/fstab aren't mounted yet at the time udevadm trigger --action=add runs. The robust way on systemd would be to add a unit with the right dependencies, I don't know how to do that. Commented Sep 28, 2015 at 7:46

1 Answer 1


Rules for add actions are executed during system startup if the device is present. This is done by running udevadm info --action=add on a SysVinit system, and internally on an Upstart or Systemd system. However this happens very early in the boot process, at a time when the root filesystem is still mounted read-only and filesystems from /etc/fstab are not mounted yet. I'm not sure if /run is already available at that point. That may well be too early for what your script needs to do.

The quick-and-dirty but portable way to run your script during startup would be to run it from /etc/rc.local, conditionally on the device being present.

if [ -e /dev/disk/by-id/… ]; then

Beware that there's a possible race condition if the device is inserted during startup: the script will be executed as soon as the device detected, and then again from /etc/rc.local.

A cleaner solution would be to define a systemd unit and declare the dependencies that you need in your script. You still need to beware of the potential race condition.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .