Monitor udev events like usb devices being connected, and execute actions upon evnet
Project description
udev_monitor
Monitor linux udev events and execute actions on detection
Designed to execute actions when USB devices are plugged-in / removed
udev_monitor works with Linux udev and monitors it's events. Upon a specific event for a given device (in VENDOR_ID:PRODUCT_ID format), it will execute an action.
Setup
pip install udev_monitor
Quickstart example
Run script /usr/local/bin/myscript.sh
with argument 0665:5161
everytime USB device with vendor id 0665 and product id 5161 is added or removed
udev_monitor.py --devices 0665:5161 --udev-actions add,remove --filters=usb --action /usr/local/bin/restart_nut_driver.sh
Full usage
--devices List of comma separated devices to monitor. Example:
'0665:5161, 8086:1234'
If no devices are given, all devices are monitored.
--udev-actions List of udev events which should trigger and action
Valid actions are: 'add', 'remove', 'change', 'online', 'offline'. Defaults to 'add, change, online'
--filters List of comma separated udev monitor filters. Filters are applied with OR logic. Example:
'usb,tty'
--action Path to script. Script will get detected device as only argument.
--timeout Maximum execution time for script
--config Optional path to config file
Optional configuration file layout
[UDEV_MONITOR]
devices = '0665:5161'
filters = 'usb'
action = '/path/to/script.sh'
udev_events = 'add'
timeout = 3600
Setting monitor up as a service
- copy file
scripts/udev_monitor@.service
to/etc/systemd/system
- Reload daemons
- Create configuration file in
/etc/udev_monitor
from example config inscripts/udev_monitor.conf.example
- Launch service
Example:
cp scripts/udev_monitor\@.service to /etc/systemd/system
systemctl daemon-reload
mkdir /etc/udev_monitor
cat << EOF > /etc/udev_monitor/udev_monitor1.conf
devices = '0665:5161'
filters = 'usb'
action = '/path/to/script.sh'
udev_events = 'add'
timeout = 3600
EOF
systemctl enable --now udev_monitor@udev_monitor1.conf
You can launch multiple udev_monitor instances by creating multiple conf files and loading them with:
systemctl enable --now udev_monitor@umy_ups.conf
systemctl enable --now udev_monitor@my_modem.conf
systemctl enable --now udev_monitor@my_harddrive.conf
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
udev_monitor-1.2.2.tar.gz
(7.2 kB
view hashes)
Built Distribution
Close
Hashes for udev_monitor-1.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f175ebea3801760046bfa1fc08eb0e9b75f27c3d32becb454c1a72fa9ad6fc61 |
|
MD5 | 410ec5da75f04b7009cfebe6fb4e644e |
|
BLAKE2b-256 | 6af8314cf2804f73af4aa0f9e8026004ae0f835b0a9d318d7b53d715792664ee |