Run shell scripts from udev rules

udev is the dynamic device manager for Linux, smartly manages different hardware devices, and it directly interacts with the Linux kernel, mode about udev here.

What if you want to run a specific program when a specific hardware added to the system ? Let say run a backup script when a pendrive is connected.

Background: I faced a strange problem on a laptop running Debian Testing, udev rules failed to switch an USB 3G dongle to modem mode from CD-ROM mode. So I decided to run a shell script whenever the 3G dongle plugged in, rather than switching it manually.

This method could be implemented almost everywhere, but I’m using the example above to make the tutorial simple.


1. Prepare the shell script

This step depends upon your requirements, write the script to do whatever you want, and save it somewhere, I’m storing it under the /usr/local/bin folder.

My script,

# Switch Micromax 3G dongle
usb_modeswitch -Q -v 1c9e -p f000 -V 1c9e -P 9605 
-M "55534243123456788000000080000606f50402527000000000000000000000"

Make it executable with chmod +x

 sudo chmod +x /usr/local/bin/switch_modem 


2. Create a systemd service

As directly running the script from a custom udev rule didn’t worked, I had to create a new systemd service to run the script through it, when the device is plugged in, a USB modem in this case.

User created systemd service files are usually stored under /etc/systemd/system/ , I’m naming it switch_modem.service

 sudo nano /etc/systemd/system/switch_modem.service 

The service file

Description=3G modem switcher



This wiki article is very helpful to understand systemd and how to create systemd services.


3. Edit or create custom udev rules

You could directly edit a desired udev rule or create a new rule under /etc/udev/rules.d/ folder, I’m creating a new rule

 sudo nano /etc/udev/rules.d/40-switch_modem.rules 

My custom rule looks like bellow

 ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="1c9e", ATTR{idProduct}=="f000", RUN+="switch_modem.service" 

Now save and exit and reload newly created rules.

 sudo udevadm control --reload-rules 

You may need to restart the udev service too, use

 sudo service udev restart  

Or use this if the service command is not available

 sudo systemctl restart systemd-udevd.service 

In my case, now the 3G modem is switching automatically, and working fine, more on how to write udev rules.


This tutorial is not limited to just switch a unsupported 3G dongle, could be repurposed for anything you want to run from an udev event, i.e. when a new hardware added to the system.

I hope you will find this tutorial helpful and it’s simple enough to understand. If you have any suggestion or question, please leave comments, I’ll be happy to hear from you 🙂 .

1 Response

  1. Emmanuel says:

    This is indeed what I need.

Leave a Reply

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