I've been trying to use udev to make a Debian system run a bash script when a wireless card is connected.

So far I created this file /etc/udev/rules.d/wifi-detect.rules:

ACTION=="add", ATTRS{idVendor}=="0cf3", ATTRS{idProduct}=="9271", RUN+="/root/test.sh"

And for now, I'm trying to make test.sh with this contents work:

/bin/echo "test!" > /test.txt

But for some reason, nothing seems to happen when I connect the wireless card, no test.txt file is created.

My lsusb on the card:

Bus 001 Device 015: ID 0cf3:9271 Atheros Communications, Inc. AR9271 802.11n

Running udevadm monitor –env this is what happens when I connect the card:

KERNEL[1017.642278] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3 (usb)
KERNEL[1017.644676] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0 (usb)
KERNEL[1017.645035] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/firmware/1-1.3 (firmware)
KERNEL[1017.708056] remove   /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/firmware/1-1.3 (firmware)
UDEV  [1017.714772] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3 (usb)
UDEV  [1017.733002] remove   /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/firmware/1-1.3 (firmware)
UDEV  [1017.772669] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/firmware/1-1.3 (firmware)
UDEV  [1017.798707] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0 (usb)
KERNEL[1018.456804] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/ieee80211/phy8 (ieee80211)
KERNEL[1018.465994] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/net/wlan0 (net)
KERNEL[1018.479878] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/leds/ath9k_htc-phy8 (leds)
KERNEL[1018.483074] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/usb_device/usbdev1.20 (usb_device)
UDEV  [1018.600456] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/leds/ath9k_htc-phy8 (leds)
UDEV  [1018.604376] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/ieee80211/phy8 (ieee80211)
UDEV  [1018.626243] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/usb_device/usbdev1.20 (usb_device)
KERNEL[1018.659318] move     /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/net/wlan1 (net)
UDEV  [1018.758843] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/net/wlan1 (net)
UDEV  [1018.932207] move     /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/net/wlan1 (net)

I've tried a lot of examples around but I can't make it work. I hope someone can help me out with this one ;) Thank you!


To simplify thing, I changed my rule to:

ACTION=="add", ATTRS{idVendor}=="0cf3", ATTRS{idProduct}=="9271", RUN+="/bin/echo 'test' > /test.txt"

I managed to set udevadm control --log-priority=info as @user1146332 suggested and I got this interesting log:

Sep  9 16:27:53 iklive-rpi1 udevd[1537]: RUN '/bin/echo 'test' > /test.txt' /etc/udev/rules.d/wifi-detect.rules:1
Sep  9 16:27:53 iklive-rpi1 udevd[1544]: starting 'firmware.agent'
Sep  9 16:27:53 iklive-rpi1 udevd[126]: seq 663 queued, 'remove' 'firmware'
Sep  9 16:27:53 iklive-rpi1 udevd[126]: seq 663 forked new worker [1547]
Sep  9 16:27:53 iklive-rpi1 udevd[1537]: 'firmware.agent' [1544] exit with return code 0
Sep  9 16:27:53 iklive-rpi1 udevd[1548]: starting '/bin/echo 'test' > /test.txt'
Sep  9 16:27:53 iklive-rpi1 udevd[1547]: seq 663 running
Sep  9 16:27:53 iklive-rpi1 udevd[1547]: no db file to read /run/udev/data/+firmware:1-1.3.4: No such file or directory
Sep  9 16:27:53 iklive-rpi1 udevd[1547]: passed -1 bytes to netlink monitor 0x1af5ee0
Sep  9 16:27:53 iklive-rpi1 udevd[126]: seq 663 done with 0
Sep  9 16:27:53 iklive-rpi1 udevd[1547]: seq 663 processed with 0
Sep  9 16:27:53 iklive-rpi1 udevd[1537]: '/bin/echo 'test' > /test.txt'(out) 'test > /test.txt'
Sep  9 16:27:53 iklive-rpi1 udevd[1537]: '/bin/echo 'test' > /test.txt' [1548] exit with return code 0

So... Isn't the return code 0 the exit code for successful completion? If so why I don't get any file on the system?


I managed to get this working using the tip by @htor. My current rule:

ACTION=="add", ATTRS{idVendor}=="0cf3", ATTRS{idProduct}=="9271", RUN+="/bin/sh -c '/bin/echo test >> /test.txt'"

But by some reason the command is executed like 8 times, is there a way to avoid this? I think is is happening because when the wireless card drivers are being load they need to virtually unmount and mount the card. Tips?

  • 1
    related to EDIT: I'm sure that the /bin/echo was executed successfully as your log suggests. The output of your command is test > /test.txt as your log states. The reason for this is, that the character > has no special meaning at all in your context. It's just the third command line argument you passed to echo. You get what you want if you let your shell interpret the given line /bin/echo 'test' > /test.txt. Like you've did in your second EDIT. For instance if you let udev execute touch /test.txt in contrast to what you've did you would see a new file in your root. – user1146332 Sep 9 '12 at 19:08

I had a similiar problem a while ago and the solution was to change the RUN+= part to RUN+="sh -c '/root/test.sh'". Now, I don't know if you need that in this case as the rule is calling a script, not a command.

Another observation: try removing the ! from the "test!" string or replace the double-quotes with single quotes. The bang ! is probably making trouble because of its special meaning in the shell and the double quotes preserves that meaning.

  • With or without the ! it's not working. – TCB13 Sep 9 '12 at 15:26
  • RUN+="/bin/sh -c '/bin/echo test >> /test.txt'"works, but by some reason e got "test" written 8 times in the file. Seem like the command is being executed multiple times :S – TCB13 Sep 9 '12 at 16:07

My advise would be to set the logging priority of udev from err to info with

 udevadm control --log-priority=info

If you would like to see even more information set it to debug. Now you can find very detailed information of what udev did in /var/log/daemon.log (At least on a debian related system). Generally that helps a lot chasing errors.

This is only a complement to htor's answer which probably solve your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.