A
A
Anton2020-09-21 10:57:27
linux
Anton, 2020-09-21 10:57:27

How to properly monitor usb connection in raspbian?

Good afternoon!
I'm not very good with bash and linux in general.
I need to somehow understand that now a new usb device (smartphone) has connected to the raspberry pi 3b+. Then connect to it via adb driver.

To do this, I found an option in an eternal loop to check how many devices are currently connected via usb (lsusb | wc -l), and if this number is more than 4 (that is, 4 usb devices are indicated in idle time, sort it out when I connect some other device appears +1) launch further actions with this smartphone.

The question is, are there more "correct" or efficient options?
I met another option through "udev" - but is there some kind of timeout for the execution of the running task?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Saboteur, 2020-09-21
@ibnsan

In the directory /etc/udev/rules.d add a file, for example
/etc/udev/rules.d/mydevice.rules:
Add the line
ATTRS{idVendor}=="152d", ATTRS{idProduct}=="2329" to it , RUN+="/path/.sh"
Then you write the actual script /path/mydevice.sh

#! /bin/sh
    env >>/path/mydevice.log
    file "/sys${DEVPATH}" >>/path/mydevice.log

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

    lsusb >> /path/mydevice.log

U
unseriously, 2020-09-21
@unseriously

Yours is probably the easiest way. There are also similar utilities: lsblk and usb-devices
but in general here: https://linuxhint.com/list-usb-devices-linux/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question