Is there any way to detect when a USB Wi-Fi adapter is physically disconnected from the Pi, and run a command when that happens? I'm using the Raspberry Pi 1, model B+, and the adapter is connected to the bottom right USB port. It's the Ralink RT5370 adapter, if it helps.

  • 2
    If you mean physically disconnected, as in pulled out, yes -- you want to look into udev rules. For an example demonstrating how to shut the pi down cleanly when a particular device is removed see here: raspberrypi.stackexchange.com/a/4722/5538 (you could do anything that way, not just run shutdown). If you mean "disconnected from the network" also yes, but the methodology would be unrelated. – goldilocks May 22 '16 at 18:11

Assuming you don't have any network equipment other that the WIFI dongle that you could unplug, you could simply write

SUBSYSTEM=="net", ACTION=="remove", RUN+="/path/to/command"

into a file named /etc/udev/rules.d/my_custom.rules. Note that you need to specify the full path to your command, since it will be executed directly, without using a shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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