1

I am trying to use udev to detect a USB attach event then run a script. There are many examples of this but I can not seem to get it to work.

My script is /usr/bin/ is very plain usbattached.sh

!/bin/bash

echo !Hello World!
mkdir /media/usb_device
mount /dev/sda1 /media/usb_device

My rule in /etc/udev/rules.d is also very simple usbconnected.rules

ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="18a5", ATTRS{idProduct}=="0243", RUN+="/bin/usbattached.sh"

I have reset the device many times in hopes udev would update. So far it has not. I also attempted to run a command to refresh the rules with no luck.

udevadm control --reload-rules && udevadm trigger

Is there something wrong with my rule/script?

Plugging my USB into my device I get the output

root@imx6ull14x14evk:/usr/bin# [  344.348033] usb 1-1: USB disconnect, device number 2
[  347.572148] usb 1-1: new high-speed USB device number 3 using ci_hdrc
[  347.796563] usb 1-1: New USB device found, idVendor=18a5, idProduct=0243, bcdDevice= 0.02
[  347.805120] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[  347.820492] usb 1-1: Product: STORE N GO
[  347.829885] usb 1-1: Manufacturer: Verbatim
[  347.839204] usb 1-1: SerialNumber: 12050602000042
[  347.864925] usb-storage 1-1:1.0: USB Mass Storage device detected

Edit: I did give my script executable permissions with chmod +x usbattached.sh

Edit 2: turning on udev debug and watching the journctl. I remove and add my USB device. I see in the log

Mar 03 15:44:00 imx6ull14x14evk systemd-udevd[270]: 1-1: /etc/udev/rules.d/usbconnected.rules:1 RUN '/bin/usbattached.sh
Mar 03 15:44:00 imx6ull14x14evk systemd-udevd[270]: 1-1: Running command "/bin/usbattached.sh"
Mar 03 15:44:00 imx6ull14x14evk systemd-udevd[270]: 1-1: Starting '/bin/usbattached.sh'
Mar 03 15:44:00 imx6ull14x14evk systemd-udevd[270]: Successfully forked off '(spawn)' as PID 275.
Mar 03 15:44:00 imx6ull14x14evk systemd-udevd[270]: 1-1: Process '/bin/usbattached.sh' failed with exit code 1.
Mar 03 15:44:00 imx6ull14x14evk systemd-udevd[270]: 1-1: Command "/bin/usbattached.sh" returned 1 (error), ignoring.

This means my script is running, but erroring out.

2 Answers 2

1

!/bin/bash

Your bash shebang is malformed (first character should be #). Also, udev will only run scripts with sh, not bash. The first thing you should do is fix the shebang to #!/bin/sh.

echo !Hello World!

  1. ! is a special character so you need to quote the string that contains it, otherwise it will get interpreted by the shell. (ex.: echo '!Hello World!'

  2. The script will not be run in an interactive shell by udev, so echo'd text will not appear anywhere. If you want confirmation the script is being run, redirect it to a file: echo '!Hello World!' > /tmp/output_$(date +%s).txt (the $(date +%s) part gives the file a unique name each time the script is run).

0

Moving the script to /home/michael solved the issue. I assume this has to do with user privilege's.

I am using a terminal to act as root during development. Running the command as myself in /usr/bin works as expected. I assume udev runs as a user that does not have root permissions.

2
  • udev runs as root but will only run scripts with usr/bin/sh, not bash. Mar 3, 2022 at 16:32
  • Why do my scripts fail to run from /etc/udev/scripts ?
    – Michael
    Mar 3, 2022 at 19:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .