24

My Pi is currently not responding to SSH, and the only HDMI monitor I have is a large TV several meters away. I've heard bad things about unplugging it, but do I have any other option?

Currently running headless raspbian, maybe I could connect a keyboard and guess about commands?

  • 3
    It'll not work for now, but you could add a button to your GPIO pins to shut it down properly in the future – Rizzle Light Dec 28 '16 at 16:15
  • As a datapoint: for several years I've been using RPis for various tasks on battery-powered mobile robots. These get their power cut often and without warning. I have yet to encounter a problem with any of the Pis / SD cards (well, other than the SD connection becoming loose from the constant vibration and wall-smashing-into). – Mike Ounsworth Dec 29 '16 at 21:39
  • You can't temporary move the HDMI monitor over a few meters and then put it back when you're done? – jpmc26 Dec 29 '16 at 22:24
  • @jpmc26 it's down a set of stairs and it's a pretty big TV. – vpzomtrrfrt Dec 29 '16 at 22:59
24

Although it should be regarded as a last resort since it does increase the risk of filesystem corruption, unplugging the cord is usually okay if the green ACT light is not flashing intensely.

By default the ACT led shows I/O activity on the SD card. If it is off, most likely the system is idle. This is not necessarily true, but killing a system that is busy with CPU intensive activity (and not I/O) is not inherently a problem anyway. The operating system does cache filesystem information in memory, meaning it can be out of sync with the actual storage on the SD card, but this should be dealt with at least every few seconds (you might sometimes be able to infer this pattern from the ACT led).

So, if it appears idle after watching it for 5-10 seconds and you have no other choice, go ahead and unplug the power. Again, this doesn't guarantee anything and there are unfortunate scenarios whereby the system may get locked up and filesystem syncing doesn't occur properly, but in most cases you should be okay.

I've probably had to pull the cord on a pi (or had power fail) over a hundred times this way and can't recall ever having grief because of it. Sometimes I will put the card in another machine and run e2fsck -f on the root filesystem, which is worthwhile. The OS will check the filesystem automatically on boot but it is possible for this check to fail (or not be done correctly) and go unnoticed.

  • 7
    The 5 to 10 sec idle period is because Linux's default setting for /proc/sys/vm/dirty_writeback_centisecs is 500 centiseconds (5 seconds). So if Linux has unwritten data sitting in I/O buffers, it will start flushing it to disk after 5 seconds, unless you changed it. In theory a journaling FS should be safe against corruption of the whole filesystem even if you poweroff during a write, but waiting for it to be idle gives you a much better chance of also not damaging the any recently-modified files. (Which journaling only protects with ext3/ext4 data=journal, which isn't the default) – Peter Cordes Dec 29 '16 at 3:17
30

Magic SysRq Key

This should get you started.

The magic SysRq key is a key combination understood by the Linux kernel, which allows the user to perform various low-level commands regardless of the system's state. It is often used to recover from freezes, or to reboot a computer without corrupting the filesystem. Its effect is similar to the computer's hardware reset button (or power switch) but with many more options and much more control.

You can plug in a keyboard and type this stuff in blind (with no monitor.)

No filesystem corruption, no extra hardware required.


GPIO Button

As Rizzle Light suggested, you could grab some sort of button and wire it to the GPIO pins, and do a little programming magic to make pressing the button execute the "poweroff" command (with admin privileges, of course.)

There are tons of GPIO programming tutorials online that you can adapt, like this one.

Rahul R Dhobi has a nice two-liner python script that will shut down a Raspberry Pi:

import os
os.system("shutdown now -h")

execute your script with root privileges.

  • 2
    The Linux kernel's magic-SysRQ documentation is the only piece of documentation I've ever printed out onto paper :). (And that was back when I only had one computer.) – Peter Cordes Dec 29 '16 at 3:19
3

No need to guess; just plug in the keyboard and type this and hit enter:

sudo shutdown -h now

The shutdown command needs a root privilege to execute.

  • 4
    You'd have to login first (blind) and either use a privileged account or (presuming Raspbian's superpowered pi user...) prepend sudo. – goldilocks Dec 29 '16 at 7:54
  • 2
    @goldilocks: You probably want to put sudo at the start of the command, rather than appending it :-) – psmears Dec 29 '16 at 10:08
  • @goldilocks: Right, still, logging in doesn't seem like that much of a chore. – Lightness Races in Orbit Dec 29 '16 at 12:38
  • 1
    I wasn't commenting on the level of difficult ;) @psmears Whoops, left you are! Corrected. – goldilocks Dec 29 '16 at 14:45
  • @goldilocks: Fair enough :) – Lightness Races in Orbit Dec 29 '16 at 14:52
1

The serial port is a good fallback for such situations, and it is enabled by default.

You could try connecting to the serial port using Putty. If that works, then login using your username and issue "sudo poweroff" to shut down safely.

See http://elinux.org/RPi_Serial_Connection for more info ...

-1

Window button -- nine time downward key -- 2 times enter... Enjoy...

  • 1
    Thanks for the answer - all inputs are appreciated. I've been asked to suggest some changes to help you improve your answer: First, I assume by "Window button", you mean the button with the Microsoft logo on Windows keyboards. If so, you should state that explicitly. Also, a key sequence is implied in your answer, but not specific enough. In short, please re-write your answer to make it unequivocally clear what your answer actually is, rather than relying on assumptions. – Seamus Jul 23 '18 at 16:14
  • Also note that the asker is running headless raspbian. That means the desktop environment is not installed, so this key sequence probably won't help. – Hydraxan14 Aug 26 '18 at 4:12

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.