Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a script that I wish to run whenever I plug/unplug a mouse or keyboard to my USB port.

There is already plenty of documentation on how to configure udev rules to run arbitrary scripts when a device is plugged or unplugged. But configuring those rules requires root permissions. And, although I do have root access on my machine, I'm looking for a solution that does not require sudo, root, or writing udev rules. (why? because it will be easier to run, maintain and distribute)

Most likely, the solution will use dbus, but I might be mistaken.

(By the way, I'm and advanced user and former Gentoo user, feel free to spit out technical details!)


Solutions that require root:

Seemingly outdated solutions that don't need root:

share|improve this question
    
askubuntu.com/questions/159007/… could help? –  Tim Aug 26 at 15:11
    
"(why? because it will be easier to run, maintain and distribute)" I would include "easier to abuse". If anyone could hook a script onto an USB device it would be far too easy to abuse. You probably will need sudo/root to set this up (and circumventing like @Tim suggest is also using root ;) ) –  Rinzwind Aug 26 at 15:11
    
@Rinzwind it would need it for set up - but most things do, like installing any program - that ^^ doesn't need root each time, or each startup.... –  Tim Aug 26 at 15:14
    
@Tim solution is overkill. It is a nice feature, indeed, but overkill. Essentially, I want to run some xset and xinput commands to reconfigure my mice and touchpad whenever one of them gets connected. –  Denilson Sá Aug 26 at 15:18
    
What do you want it to do with the information that one has been unplugged? Would a script running every 30 secs comparing the difference between xinput then and now work? –  Tim Aug 26 at 15:22

1 Answer 1

up vote 1 down vote accepted

The "How can I listen for 'usb device inserted' events in Linux, in Python?" question has a very short sample Python script using pyudev. That script can easily detect when a device is connected or disconnected, by monitoring the device-event from the usb subsystem.

What's more, pyudev is pure-python implementation and is available on both Python 2 and Python 3.

Based on that sample code, I've written auto_exec_xinput_xset_upon_usb_device_change.py (see also the most recent version). Works on both Python 2 and Python 3.

#!/usr/bin/env python

import functools
import os.path
import pyudev
import subprocess


def main():
    BASE_PATH = os.path.abspath(os.path.dirname(__file__))
    path = functools.partial(os.path.join, BASE_PATH)
    call = lambda x, *args: subprocess.call([path(x)] + list(args))

    context = pyudev.Context()
    monitor = pyudev.Monitor.from_netlink(context)
    monitor.filter_by(subsystem='usb')  # Remove this line to listen for all devices.
    monitor.start()

    for device in iter(monitor.poll, None):
        # I can add more logic here, to run only certain kinds of devices are plugged.
        call('foobar.sh')


if __name__ == '__main__':
    main()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.