First time poster, not first time RasPi user.

I am trying to figure out a failsafe way to shutdown any Pi(w/ rasbian) due to the oft-sited issues with SD card corruption when the power cable is yanked out.

This issue has been addressed many times in the past, the strategies I most often see are a python script and a GPIO button, or with a separate micro-controller via UART or SPI.

I would prefer to find a way of doing so via USB because it doesn't require first installing a script(python/GPIO) or plugging something into the GPIO(UART/SPI). Likewise, if you would like to shutdown a Pi, you just plug in the USB stick(and maybe press a button on said stick)

So, would it be possible to implement something like this on some sort of USB stick?

My first thought was a USB flash drive with an autorun shell script that would run anytime it was plugged into a Pi. I am no Linux guru, but I did some digging and it seems in order for this to work, the Pi's system must first be modified to accept autorun scripts(something to do with UDEV?). This in not ideal because I would like to be able to plug it into any pi(like one with a brand new copy of rasbian) and cause a shutdown.

My next thought was to use something like an Atmega 32u4 on a USB stick to implement a shutdown, but I do not know much about USB protocols so I am not sure where to start. My most basic implementation(far from ideal), would be to use the 32u4 as a USB HID via Arduino to send the keystrokes to open a terminal and enter "sudo shutdown -h now". This might very well work fine, I will test it out when I get home, but it seems pretty janky and I'm sure there's a better way.

Would anyone be able to point me down the right path?


2 Answers 2


I see two ways of doing this:

1. UUID Based

You could write a very simple script, possibly in Python, to check for a specific UUID to be plugged into the Pi. The script would then shutdown the Pi.

It would go something like this:

import os
import time
while True:
    if "123456789-1234-1234-1234-1234567890" in os.popen("ls /dev/disk/by-uuid/").read():
        os.system("sudo halt")

2. USB Rubber Ducky

A USB Rubber Ducky is a flash drive-looking device that is actually a keyboard emulator. They're quite expensive for your needs and annoying, but it would work.


Instead of a $50 Rubber Ducky you could get a $5 Arduino Beetle (a clone of Leonardo which fits directly into a USB socket):

enter image description here

Look into Arduino Keyboard samples to see how it should be programmed. I have used one such board to send Ctrl+Alt+Del, password, Enter to log into a Windows PC.

In your case, you'll be sending something like Ctrl+Alt+F1, sudo shutdown now, Enter, assuming you're already logged in on the virtual terminal.

You could also do the same using an udev rule, but in my opinion this approach will be less flexible and harder to debug.

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.