Run script on USB detection

Raspberry Pi 3 is running squeezelite client for Squeezebox, and connected by USB to Audiolab M-DAC (sounds great!) I found that if I turned off the M-DAC at night (but not the pi), when I turned it back on then squeezelite would not work until manually restarted. To solve this there is a script which detects the USB connection and restarts squeezelite.

List all USB devices.

pi@raspberrypi:/etc/udev/rules.d $ lsusb
Bus 001 Device 004: ID 0451:adac Texas Instruments, Inc.
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Devices are managed by the udev daemon. Create a udev script referencing vendor and product ID reported by lsusb above.

pi@raspberrypi:/etc/udev/rules.d $ cat 00-usb-audiolab.rules
ACTION=="add", ATTRS{idVendor}=="0451", ATTRS{idProduct}=="adac", RUN+="/usr/local/bin/usb-audiolab-in_udev"

Script called by the udev script above, which in turn calls another script to complete the desired action

pi@raspberrypi:/usr/local/bin $ cat usb-audiolab-in_udev
#!/bin/bash
/usr/local/bin/usb-audiolab-in &

The final script, which restarts squeezelite.

pi@raspberrypi:/usr/local/bin $ cat usb-audiolab-in
#!/bin/bash
sleep 1
echo "restarting squeezelite..."
/etc/init.d/squeezelite restart
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s