0

I'm trying to run a shell script when my iPhone connects to my Raspberry Pi, running Raspbian

Here's what I have so far: at /etc/udev/rules.d/test.rules:

ATTRS{idVendor}=="05ac", ATTRS{idProduct}=="1227", RUN+="/home/pi/testscript.sh"

How can I check that this script is actually running, because I don't think it is, and is this the correct syntax?

Charlie
  • 1
  • 1

2 Answers2

2

Your script must be executable.

Do this step by step to find out your problem:

First create a simple script:

sudo nano /bin/device_added.sh

Add the following lines in the device_added.sh script

#!/bin/bash echo "USB device added at $(date)" >>/tmp/scripts.log

Open the second script.

sudo nano /bin/device_removed.sh

Then add the following lines to device_removed.sh script.

#!/bin/bash echo "USB device removed at $(date)" >>/tmp/scripts.log

Save the files, close and make both scripts executable.

$ sudo chmod +x /bin/device_added.sh $ sudo chmod +x /bin/device_removed.sh

Next, let’s create a rule to trigger execution of the above scripts, called /etc/udev/rules.d/80-test.rules

nano /etc/udev/rules.d/80-test.rules

Add these two following rules in it.

SUBSYSTEM=="usb", ACTION=="add", ENV{DEVTYPE}=="usb_device", RUN+="/bin/device_added.sh" SUBSYSTEM=="usb", ACTION=="remove", ENV{DEVTYPE}=="usb_device", RUN+="/bin/device_removed.sh"

Save the file and close it. Then as root, tell systemd-udevd to reload the rules files (this also reloads other databases such as the kernel module index), by running.

sudo udevadm control --reload

Now connect your iphone or any other usb drive into your machine and check if the device_added.sh script was executed. First of all the file scripts.log should be created under /tmp.

Then the file should have an entry such as “USB device removed at date_time”

If this steps worked correctly the you can replace the /bin/device_added.sh script with your script. and place your rule in /etc/udev/rules.d/80-test.rules

udev-for-device-detection-management-in-linux

1

Run udevadm monitor and plug the USB device. It will show you what exactly udev does.

V13
  • 4,749