How to check if a udev rule fired?

15

3

I am trying to acquaint myself with udev, under Ubuntu 13.10.

Here is my first simple 95.usbbackup.rules rule:

ACTION=="add", SUBSYSTEMS="usb", RUN+="/usr/local/bin/my_backup.sh"

and here is the script (which has been chmodded +x) my_backup.sh :

#!/bin/bash
touch /tmp/test

Nothing at all happens when I plug in external drives. How can I check (a log, a command, anything) if the rule fired?

Thanks a lot

pouzzler

Posted 2013-11-17T15:59:03.890

Reputation: 285

2I think you mean SUBSYSTEMS=="usb". I.e. double == which tests for equality rather than single = which assigns a value to a key. – Lqueryvg – 2015-09-26T13:31:44.900

Answers

6

I'm pretty sure this should work. Did you reload your udev rules after editing your rules?

udevadm control --reload-rules && udevadm trigger as root.

Redsandro

Posted 2013-11-17T15:59:03.890

Reputation: 496

4This doesn't really answer the question. How do you know if the rules triggered? – DanielSank – 2018-04-06T23:40:24.233

1You know because the script is run. You can make it write to a logfile. Also udevadm monitor – Redsandro – 2018-04-07T14:15:25.743

I made a narrower rule : KERNEL=="sdb", which works. Does udev only treats rules that identify some hardware uniquely? – pouzzler – 2013-11-30T14:29:54.923

No, try KERNEL!="sdz*" and you should get everything (except sdz[1-9]) – Redsandro – 2013-11-30T23:39:35.230

3

You can give a command as root like this:

udevadm monitor

It will show when a rule has fired.

user2703782

Posted 2013-11-17T15:59:03.890

Reputation: 55

10udevadm monitor just shows the udev events, but not if a corresponding rule was fired. However, you can look for the event which should trigger the rule, but then you do not know if your rule works. – F.Raab – 2018-06-20T14:18:31.237

2

I'm running kernel 3.0.35, but the following works for me.

To get the path for the device you can do something like this:

udevadm info --name /dev/sda1 --query all

You will get more information than you need but you're interested in the DEVPATH. Then to see what udev rules are executed you run this:

udevadm test DEVPATH

I don't think this actually executes the rules, the documentation says this 'simulates' the events for the given device. To get more information, check out this man page: https://www.freedesktop.org/software/systemd/man/udevadm.html

JSunderland

Posted 2013-11-17T15:59:03.890

Reputation: 21

1

With udev / systemd version 241 and similar, as root:

udevadm control --log-priority=debug
journalctl -f

Or to make it permanent, again as root:

vi /etc/udev/udevd.conf
systemctl restart systemd-udevd
journalctl -f

PS: the most frequent answer looks like:

udevadm -d test /devices/where/is/my/device |& less

... but this has a number of issues. The main ones:

  • where/is/my/device ? Tedious, complicated and error-prone.

  • Comparing old answers to recent udev version 241 output, udevadm test seems to show less information that it used to.

  • It's only a simulation!

Every time it warns:

This program is for debugging only, it does not run any program
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.

udevadm test is for developing a new rule, it's not for troubleshooting broken, missing or overridden rules.

MarcH

Posted 2013-11-17T15:59:03.890

Reputation: 173

0

I was having the same problem with Raspberry Pi 3 B+. I was trying to invoke a script on inserting a USB storage device. The rules do not get logged in syslog, so it becomes very difficult to understand which rule worked or which rule failed.

So I did the following:

  1. I made my rule file in /etc/udev/rules.d/100-myrule.rules
  2. Then I ran the command sudo /etc/init.d/udev restart

and when I checked, it worked.

A piece of information, that may or may not be useful, is that the filesystems are readonly for udev until the command in step 2 is executed.

MSharq

Posted 2013-11-17T15:59:03.890

Reputation: 1