Ubuntu: How to automatically launch scripts/software upon inserting USB devices

Have you ever thought about performing a custom action or launch a specific software when plugging your favorite USB device on Ubuntu?

USB Cable

I’ve been investigating this possibility recently and I’ve found a quite satisfactory solution. The idea has born when reading this blog about how to read udev rules. Udev is the device manager introduced with Kernel 2.6.13 and it manages all the /dev devices dynamically. Using upstart rules, we can use specific files (rules) to tell to our OS that a specific action/script should be run when a device (or a specific device) is attached to the system. The files are

So I thought that it would be good to launch a simple script when I attach my Palm Pre2 to my Ubuntu via USB and run a rsync that will maintain a local backup of my USB partition on the phone.

Thanks to the work done at stackoverflow I was able to understand how to create the rules, tailoring them for my device and then use notify-send to have a graphical view of what the script is doing.

First thing to do is to identify your device:

launch from a terminal window the command: udevadm monitor –env

This will monitor the ports of your machine and when you plug a device like an usb pen, you’ll see a few lines identifying different pieces of information.

What you need to write down are: ID_SERIAL_SHORT, idVendor and product.

For example our ID_SERIAL_SHORT can be something like: 156aab412740f93d04869cac6c0837229d48456e, the idVendor a small number like “8332” like the product “8045”.

Now that we have this information, we can create our upstart rule by creating a file named like 91-backup.rules in /etc/udev/rules.d (please note that the file should start with two digits and having an higher number of the other files in the same directory, so you’ll be sure that it’s executed first).

Mine looks like:

SUBSYSTEM==”block”, ID_SERIAL_SHORT==156aab412740f93d04869cac6c0837229d48456e, NAME=”PALM PRE”, SYSFS{idVendor}=”8332″, SYSFS{product}=”8045″,


Please note the last line that calls a specific script, but you can choose a software or anything else.

In my case, the content of that “copy.sh” is:


sleep 2 #wait for the system to mount the drive

 if [ -d /media/PALM PRE/ ] # if the drive has been mounted


 su stefano -c “DISPLAY=”:0.0″ notify-send “Mount” “Palm_PRE2 ready” -t 2000 -i “/home/user/backup/pre.jpeg”” #show on the OSD a specific message with icon

 rsync -ru /media/PALM PRE/ /home/user/backup/PRE/ #do a backup of my external media using rsync

        su stefano -c “DISPLAY=”:0.0″ notify-send “Palm_PRE2” “Syncronization_complete” -t 2000 -i “/home/user/backup/images.jpeg”” #reports that the backup is completed on the OSD


I hope that this information will help you in doing something similar (and useful!) on your system. Enjoy!


3 responses to “Ubuntu: How to automatically launch scripts/software upon inserting USB devices”

  1. Hi,

    I tried your instructions to startup a backup script, but I have some problems. I have the following lines in my rules script:

    root@unixworldnas:/media# cat /etc/udev/rules.d/91-backup.rules
    SUBSYSTEM==”block”, ATTRS{idVendor}==”0480″, ATTRS{idProduct}==”a006″,


    My system wil not automount the usb device so it backups to a local folder instead of my usb device.

    When I do the following line (1 line without empty lines), it mounts perfectly, but doesn’t start the script:

    root@unixworldnas:/media# cat /etc/udev/rules.d/91-backup.rules
    SUBSYSTEM==”block”, ATTRS{idVendor}==”0480″, ATTRS{idProduct}==”a006″, RUN+=”/root/scripts/backup_to_usb”

    Do you know how i can fix my problem?

    1. Stefano Prenna Avatar
      Stefano Prenna


      try with:

      echo ‘SUBSYSTEM==”block”, ATTRS{idVendor}==”0480″, ATTRS{idProduct}==”a006″,
      RUN+=”/root/scripts/backup_to_usb”’ > /etc/udev/91-backup.rules

      otherwise try to use the syntaxes suggested in my example. Another option is to use a software like “Cuttlefish” that will allow you to do the same using a GUI.

      Hope it helps!

  2. Thanks for your reply.

    I tried all you options, but no lukc. I did not installed cuttlefish, because I use an Ubuntu 12.04 server without a gui. I wil try some other options tonight. If I find a solution, I will post it here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.