Udev rule to run a script after plugging in USB device on Ubuntu Linux operating system

Logitech QuickCam E3500 Web CamFor my video chat sessions I use my Logitech E3500 USB web cam. On my Linux box this hardware device sort of works fine, but it does have it's own set of quirks I've developed workarounds for over the years. One of the things that bug me is that even though you can change it's configuration options trough GUI or CLI, this hardware device has it's defaults, and these defaults are back every time you plug it in. In this article I'll show you how to create script that changes web cam settings using CLI tools I've explained in one of my earlier articles, and how to make this script being executed every time web cam is plugged in.

Creating script to be executed

In this step you create script for whatever you need to be done after your device is plugged in. I'll describe my use case but yours will probably be different.

First I don't like my web cam using 60Hz setting for reducing artificial light sources induced flickering by default, because where I live we use 50Hz power sources. Secondly I would like exposure auto priority setting disabled to make my web cam image more natural. To achieve this I usually use v4l2-ctl command line program from v4l-utils package. Logitech E3500 web cam sometimes freeze due to snd-usb-audio module so my script also contains logic to work around this. I'll first install v4l-utils like this:

sudo apt-get install v4l-utils

Now I can place my v4l2-ctl commands inside /usr/bin/logitech_e3500.sh file. Logger command is used to write to syslog when script is triggered or when commands produce error:

logger "Logitech E3500 detected, adjusting configuration options..."
v4l2-ctl -c power_line_frequency=1 2>&1 | logger
v4l2-ctl -c exposure_auto_priority=0 2>&1 | logger
logger "Logitech E3500 bug #459445 detection..."
sleep 10
number_of_string_occurences=$(cat /var/log/kern.log | tail -30 | grep -iw "cannot set freq 16000 to ep 0x86" | wc -l)
if [ $number_of_string_occurences -ge 3 ]
        logger "Logitech E3500 bug #459445 detected! Working around it!"
        sleep 5
        rmmod -w snd-usb-audio
        sleep 7
        modprobe snd-usb-audio
        logger "Logitech E3500 bug #459445 NOT detected!"

I'll also make this script executable:

sudo chmod +x /usr/bin/logitech_e3500.sh

Creating udev rule to trigger script

First thing to do is to find out a few things about my web cam. I'll use lsusb command to do this. It's output might look like this:

Bus 001 Device 004: ID 046d:09a4 Logitech, Inc. QuickCam E 3500

I'll write down 046d:09a4 part because I'll need it to target my USB device while creating udev rule. Now it's time to create the udev rule. I place following inside /etc/udev/rules.d/85-my_e3500_rule.rules file:

ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="046d", ATTR{idProduct}=="09a4", RUN+="/usr/bin/logitech_e3500.sh"

Please note ATTR{idVendor}=="046d" and ATTR{idProduct}=="09a4" parts where I target the device this rule is created for.

Now it's time to put your rule to test. First let's restart udev service to pickup our new rule. Now you can use tail command to watch syslog while plugging your device in to monitor your script in action.

sudo service udev restart
tail -f /var/log/syslog

If you've configured all settings correctly and if you script is using logger command you should see your log status messages being printed after plugging in the device.

That's it. Enjoy!


A quality focused Magento specialized web development agency. Get in touch!

3 thoughts on “Udev rule to run a script after plugging in USB device on Ubuntu Linux operating system

  1. Ingo

    Many thanks for that workaround!

    I just applied it in my Debian-Wheezy where I randomly observe that quirk after booting up (microphone dead) and “cannot …” is 20 times logged.

    It only happens when the webcam (a E3500 as well) is plugged in during boot-up and only now and then – guess every 5-10 boots. I did try a lot of other configuration tuning, but none could resolve this nasty bug. If you don’t get another posting from me, everything is fine.


  2. Ingo

    On thing has to be added:

    On my system (Intel DH77EB MoBo) I don’t have any PS/2-Port, so my keyboard is attached to a USB-port. USB is of course enabled at a verry early stage of the boot process. This, when all is fine with sound-usb-audio will give *no* entries in any log (syslog not yet available).

    When snd-usb-audio has failed, this perfectly gets fixed with your sript and corresponding syslog entries show up.

    One more observation: installing ‘v4l2ucp’ package for modifying webcam settins results in *always* happening the bug and it cannot be cured later on by executing the script. Purged vl2ucp again and all is fine.

  3. Ingo


    Your setup in general works fine, unless the webcam is already plugged in during boot. The udev-rule terribly delays the boot process and does not catch the webcam whatever delays I inserted.

    I now replaced the udev-rule by a few lines in /etc/rc.local:

    if [ -e “/dev/video0” ]; then
    sleep 25
    logger “/dev/video0 not found, Logitech webcam not present”

    Works fine for me.

    I did not find a better way to detect whether the webcam is plugged in as checking fpr its video device. The delay of 25s is needed to ensure the log entries “cannot set …” are written to syslog.


Leave a Reply

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