I have a Raspberry pi b with an SD card reader connected to one of the USB ports. I would like to automatically process files as soon as an SD is inserted. Is there a way to somehow register a program to execute when a new USB disk is detected?

I basically want to use the RPI as a smart SD reader, so the SD reader will be fixed on the one USB port. And I want IoT like behavior with no user interaction ( other than inserting the SD ).

I am trying to avoid an infinite loop checking when one of the /media/usb? directories become valid.

I am new at RPI and not a low lever Linux guy, so any direction will help. Thanks,

  • The GUI automatically mounts inserted devices in /media (I generally find this annoying, and disable with fstab entries), this does not happen on the command line. I assume this uses the udev Dynamic device management rules to achieve this. You could establish udev rules to perform a specific action.
    – Milliways
    Jan 3, 2016 at 11:27
  • @davka Yes. Investigate udev. I have an example of using it for a different purpose here, but I also use the same basic methodology for running a script whenever I plug a USB HD into the pi. Beware the trigger for what you want to do would be more general than what's there. However, there are hundreds of tutorials and discussions of how to do this online -- just search "linux udev". Do not include "raspberry pi".
    – goldilocks
    Jan 3, 2016 at 14:08
  • @goldilocks Your point to udev and your post are extremely useful. I don't have access to the RPi right now, but will try udevadm monitor --udev --property as soon as I can. In particular, I want to understand if removing the SD card itself will trigger an event, or only when I remove the card reader. But +1 on your comment
    – dkarchmer
    Jan 6, 2016 at 0:17

1 Answer 1


Assuming you have no other disks plugged in, you could use a very simple python script. Something like:

import os
import time
while True:
    disks=os.system("ls /dev") #checks /dev for a disk, the first disk is always listed as /sda and /sda1
    if "sda1" in disks:
        os.system("sh YOURPROGRAM.sh") #you can put whatever command/program you want it to execute when the drive it mounted here
    time.sleep(0.5) #waits half a second before beginning the next loop, helps reduce strain on the CPU

You would have to run this script using sudo because it needs root privileges to mount disks. sudo python foo.py would work fine.

Unfortunately there is no way to avoid an infinite loop here that I know of, but checking /dev instead of /media might be easier because it does not require the disk to be mounted.

No matter where the disk is automounted, the device will always show up in /dev/sda and /dev/sda1 assuming it is the first drive put into the Pi.

  • The /media/usb0 seems to be auto-mounted. Why is re-mounting myself any better? And, what happens once I pull the SD card? Does /mnt/temp stay mounted? Or if it stays on an illegal state, how do I detect it?
    – dkarchmer
    Jan 3, 2016 at 7:35
  • @davka, huh my Pi does not automount them. You can modify the script to check /media instead, also, before removing the media you should eject/unmount the disk by using the command sudo umount /mnt/temp or whatever folder they're mounted on Jan 3, 2016 at 7:38
  • Yes, I don't know if I previously installed something that is doing this, but I can unplug the usb drive, and 'ls /media/usb0' is empty. Plug it in again, and 'ls /media/usb0' shows the SD files.
    – dkarchmer
    Jan 3, 2016 at 7:43
  • @davka that means it is automounting the disk, which is what you want, that is ok. I will update my answer with a script that will work for you. Jan 3, 2016 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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