view · edit · print · history

Run software when a (USB) device is added or removed.

This HowTo describes how to run your own script when a device is added or removed. This is often also referenced as hotplugging.

This page relates to OpenSlug (at least 2.5). Debian also supports hotplugging in basically the same way, but this page was not written with Debian in mind.

Note that this has only been tested for USB. If you have info on other buses please add.

If a device is inserted OpenSlug (at least 2.5) executes the program /sbin/hotplug with only one parameter (the bus that has detected the hotplug, e.g. 'usb' or 'pci'). Through environment variables this program gets information on whether this is an addition or removal, which device id etc etc. (for details see below).

/sbin/hotplug will dispatch this to the script or program for the specific bus. For OpenSlug 2.5 and USB it executes /etc/hotplug.d/usb/usb.hotplug. It will also execute /etc/hotplug.d/default/default.hotplug. The latter can be used to deal with unknown devices.

Within usb.hotplug you can basically do whatever you want to. Typically you want to decide on what device was added and take an appropriate action.
A very crude file would look like:
cd /usr/photos
gphoto2 -P
If a digital camera was connected this would copy all pictures from it to the /usr/photos directory (assuming your cam was supported etc).
This is a very basic script as it does not test whether a camera is connected or something else. In real life you probably want to add tests for this.

While creating the script do not forget the #!/bin/sh and don't forget to make the script executable. Of course you can also scribble a (C) program.

Environment variables passed to usb.hotplug

The following environment variables are passed to usb.hotplug. These can be used in your script to decide what action to take. The environment variables were dumped after connecting my digital camera. Note that some of these environment variables (e.g. PATH and HOME) are there always and are not related to USB. I've left them all in for completeness (and because I don't know exactly which ones are from the kernel and passed through the hotplug program).

PS1='\w \$ '
PS2='> '
PS4='+ '

view · edit · print · history · Last edited by endecotp.
Originally by effem.
Page last modified on January 10, 2006, at 05:34 AM