daw
Posts: 6
Joined: Fri May 03, 2013 11:32 am

Safe shutdown by unplugging a specific USB device.

Fri May 03, 2013 12:16 pm

Hi everybody, this is my first post here.
I want to show you an idea, hope to read your feedbacks.
Basically, it consists in a tool that lets you safely shutdown a RP by unplugging a specific USB device. It comes handy to avoid hard-shutdown by unplugging power supply when you use a headless RP-based system, e.g. a print server, and you can't (or you don't know how to) connect over SSH and run the proper shutdown command.

Based on `udev`, mechanism is simple:
1) User configures the desired USB device to be used as a "shutdown handler", and it can be a flash/external drive, SD/MMC adapter, WiFi adapter, printer, etc.
2) This tool silently waits for the "Removed Device" message and if the removed device is the preferred device then it run `shutdown -h now`

I wrote a more explanatory post on my blog: "How I shutdown my Raspberry Pi" and released source code on GitHub: unplug2shutdown.

What you guys think? is it worth spending work on?

Ciao!

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Safe shutdown by unplugging a specific USB device.

Fri May 03, 2013 1:47 pm

It should work fine.

I have done something similar using udev rules (although not generated a configurable tool to do it), where my Pi shuts down if I plug in a specific memory stick (an old 128MB one, that is too small to be any use now).

daw
Posts: 6
Joined: Fri May 03, 2013 11:32 am

Re: Safe shutdown by unplugging a specific USB device.

Fri May 03, 2013 4:59 pm

rpdom wrote:It should work fine.

I have done something similar using udev rules (although not generated a configurable tool to do it), where my Pi shuts down if I plug in a specific memory stick (an old 128MB one, that is too small to be any use now).
Thanks for reply rpdom. Catching the 'Add Device' action is a good option as well, I could implement it in the tool. I've tested it with a printer by far, turned out a really handy one-shot-deal: you press the printer's power off button and you turn off both.

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Safe shutdown by unplugging a specific USB device.

Fri May 03, 2013 6:31 pm

rpdom wrote:It should work fine.

I have done something similar using udev rules (although not generated a configurable tool to do it), where my Pi shuts down if I plug in a specific memory stick (an old 128MB one, that is too small to be any use now).
Fabulous idea, I never thought of that. I'd probably parse a tail of dmesg or something to achieve it.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Safe shutdown by unplugging a specific USB device.

Fri May 03, 2013 7:05 pm

pluggy wrote:
rpdom wrote:It should work fine.

I have done something similar using udev rules (although not generated a configurable tool to do it), where my Pi shuts down if I plug in a specific memory stick (an old 128MB one, that is too small to be any use now).
Fabulous idea, I never thought of that. I'd probably parse a tail of dmesg or something to achieve it.
Basically I put this in a rules file in /etc/udev/rules.d

Code: Select all

KERNEL=="sd*1",ACTION=="add",ENV{ID_FS_UUID}=="3c8dd6cc-0fef-4ad9-b58f-638f1e8491dc",RUN="/sbin/shutdown -h now"
Just create a single partition with an ext 2/3/4 filesystem on the USB stick, get the UUID with the blkid command and substitute for the one in my example. Obviously there are many other parameters you could match on.

User avatar
aelsas
Posts: 64
Joined: Fri Jul 06, 2012 11:23 pm
Location: Frankfurt, Germany
Contact: Website

Re: Safe shutdown by unplugging a specific USB device.

Fri May 03, 2013 11:16 pm

Great idea, thanks for sharing!

Alex

Dilligaf
Posts: 283
Joined: Wed May 23, 2012 6:48 pm

Re: Safe shutdown by unplugging a specific USB device.

Fri May 03, 2013 11:56 pm

But a lot of times plugging in ANY usb device will cause a shutdown/reboot :)

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13100
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Safe shutdown by unplugging a specific USB device.

Sat May 04, 2013 3:09 pm

Dilligaf wrote:But a lot of times plugging in ANY usb device will cause a shutdown/reboot :)
yes, unless the device in question is behind a hub. The PI itself doesn't have rush-in-current protection, but most hubs do.

smahendra
Posts: 1
Joined: Mon Sep 09, 2019 11:20 am

Re: Safe shutdown by unplugging a specific USB device.

Mon Sep 09, 2019 11:22 am

Thanks for this. I am able to shutdown my headless Pi Zero W while switching my HP printer off automatically. Although it was a bit tricky to identify parameters to trigger shutdown.

Return to “General discussion”