USB devices UDev and D-BUS
The udisks D-Bus service, obviously, only reports disks.
Just monitor udev directly (through libudev, through pyudev).
import pyudev
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
observer = pyudev.pygtk.GUDevMonitorObserver(monitor)
observer.connect('device-added', device_added_callback)
observer.connect('device-changed', device_changed_callback)
monitor.enable_receiving()
mainloop = gobject.MainLoop()
mainloop.run()
Run something when USB device is plugged in doesn't work
I'm not an expert and this isn't an answer, but I've found the following steps useful in identifying the appropriate attributes to trigger on:
- Locate the device path using
udevadm
,lsusb
, orusb-devices
. I normally just uselsusb
and let tab completion in my shell guide me. In my case, the path is/dev/bus/usb/003/007
. - Use
udevadm
to identify the device attributes for rule writing. In my case, I usedudevadm info -a --attribute-walk --root --name=/dev/bus/usb/003/007
. - Write the rule and check that it's triggering. In my case, I'm just changing the device owner to user "stephen" and it's very easy for me to check if it's working by using
ls -l /dev/bus/usb/003/007
. My rule for this case looks like:SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", OWNER="stephen"
. I have a similar rule that puzzled me for a little while because the subsystem was expectingATTRS
notATTR
, which is why I recommend walking the attributes. The rule in this latter case became: `SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", OWNER="stephen".
And, of course, man udev
is always helpful. As you said, you should struggle to identify that your rule is triggering properly and may be best off just doing a quick ownership change on the device as I did for a first step. You can run into trouble with bad attributes or symbolic links sometimes and it's
Related Topics
What Is the Naming Convention in Python For Variable and Function
Schedule Python Script with Crontab
Python Convert Microsoft Office Docs to Plain Text on Linux
How to Convert .Pcm Files to .Wav Files (Scripting)
I Cant Init Google Cloud Sdk on Ubuntu
A Simple Python Deployment Problem - a Whole World of Pain
Multiprocessing.Pool Spawning New Childern After Terminate() on Linux/Python2.7
How to Run Python Script on Usb Flash-Drive Insertion
Process Dies, If It Is Run via Paramiko Ssh Session and with "&" in the End
How to Connect to Flask Local Server
How to Get the Full Path of the Current File'S Directory
How to Push a Subprocess.Call() Output to Terminal and File
Running Process of Remote Ssh Server in the Background Using Python Paramiko
Better Way to Script Usb Device Mount in Linux
Where Does Python Tempfile Writes Its Files
Cat, Grep and Cut - Translated to Python
Using Sudo Inside Jupyter Notebook's Cell
Using Pyinotify to Watch for File Creation, But Waiting for It to Be Completely Written to Disk