Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to execute a script when I plug in a device in my Linux machine. For example, run xinput on mouse or a backupscript on a certain drive.

I have seen a lot of articles on this, most recently here and here. But I just can't get it to work.

Here's some simple examples trying to get at least some kind of response.

/etc/udev/rules.d/test.rules

#KERNEL=="sd*", ATTRS{vendor}=="*", ATTRS{model}=="*", ATTRS{serial}=="*", RUN+="/usr/local/bin/test.sh"
#KERNEL=="sd*", ACTION=="add", "SUBSYSTEM=="usb", ATTRS{model}=="My Book 1140    ", ATTRS{serial}=="0841752394756103457194857249", RUN+="/usr/local/bin/test.sh"
#ACTION=="add", "SUBSYSTEM=="usb", RUN+="/usr/local/bin/test.sh"
#KERNEL=="sd*", ACTION=={add}, RUN+="/usr/local/bin/test.sh"
KERNEL=="sd*", RUN+="/usr/local/bin/test.sh"
KERNEL=="*", RUN+="/usr/local/bin/test.sh"

/usr/local/bin/test.sh

#!/usr/bin/env bash
echo touched >> /var/log/test.log

if [ "${ACTION}" = "add" ] && [ -f "${DEVICE}" ]
then
    echo ${DEVICE} >> /var/log/test.log
fi

The rules folder is watched by inotify and should be active immediately. I keep replugging my keyboard, mouse, tablet, memorystick and usb-drive, but nothing. No log file touched.

Now, what would be the most simple way to at least know something is working? It's easier to work from something that's working than from something that's not.

share|improve this question

migrated from stackoverflow.com Feb 23 '13 at 20:45

This question came from our site for professional and enthusiast programmers.

    
Didn't you mean to post on Unix & Linux? What's your kernel version? Did you run udevadm trigger or plug a device in to apply the new rule? –  Gilles Feb 22 '13 at 15:41
    
Yes, I do that after every edit of the rules to try them out. I edited the question accordingly. This is the way udev works for a while now, but I am running 3.5.0-23-generic. –  Redsandro Feb 22 '13 at 16:12

1 Answer 1

up vote 1 down vote accepted

If you want to run the script on a specific device, you can use the vendor and product ids

/etc/udev/rules.d/test.rules:

ATTRS{idVendor}=="152d", ATTRS{idProduct}=="2329", RUN+="/tmp/test.sh"

test.sh:

#! /bin/sh

env >>/tmp/test.log
file "/sys${DEVPATH}" >>/tmp/test.log

if [ "${ACTION}" = add -a -d "/sys${DEVPATH}" ]; then
    echo "add ${DEVPATH}" >>/tmp/test.log
fi

With env, you can see what environment is set from udev and with file, you will discover the file type.

The concrete attributes for your device can be discovered with lsusb

lsusb

gives

...
Bus 001 Device 016: ID 152d:2329 JMicron Technology Corp. / JMicron USA Technology Corp. JM20329 SATA Bridge
...

share|improve this answer
    
This is interesting! It seems that it has no permission to write to /log/. It does write to /tmp/. I guess it had no permission to read my previous testscripts either. –  Redsandro Feb 22 '13 at 16:20
    
@Redsandro This was not intentional, just for, well, testing purposes. Anyway, I'm glad it helped. ;-) –  Olaf Dietsche Feb 22 '13 at 16:24
    
I would like to encourage you to also check out this question and see if your knowledge can be valuable there. :) –  Redsandro Apr 16 '13 at 9:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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