33

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?

vpzomtrrfrt
  • 441
  • 1
  • 4
  • 6
  • 4
    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

8 Answers8

36

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.


Edit in 2021; Raspberry Pi 4 with Raspberry Pi OS:

The SysReq key, by default, seems to be Right Alt + PrintScreen held down together.

The graceful shutdown key sequence is "REISUO".

So, all together:

Keep holding down Right Alt + PrintScreen, and with your other hand, press the sequence keys, leaving one second wait time between each keystroke: R + E + I + S + U + O


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.

Levente
  • 103
  • 4
Hydraxan14
  • 1,836
  • 1
  • 11
  • 23
  • 3
    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 03:19
32

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.

goldilocks
  • 58,859
  • 17
  • 112
  • 227
  • 10
    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 03:17
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.

sohnryang
  • 103
  • 4
2

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 ...

user59398
  • 21
  • 1
1

I was testing a fileserver in a part of the house without easy access to a monitor when I ran into the same issue. The following worked for me, assuming your Pi is booting to the default GUI environment:

  1. Plug in a keyboard (if not already done).

  2. Hit Alt+F2 to open a Run dialog.

  3. Type the following and hit Enter to start a Terminal window:

     lxterminal
    
  4. Wait a few seconds to be safe, and then type the following:

     sudo shutdown now -h
    
  5. Hit Enter.

This should shut down the Pi. If it works, the keyboard lights and any attached USB devices will shut off. If not, you can repeat the steps above in case you made a mistake typing the commands.

ggorlen
  • 105
  • 5
Nealios
  • 11
  • 1
0

Mouse with shell script on desktop

My setup is no monitor, keyboard or GPIO and I'm potentially SSH'd in to the Pi via Ethernet. Sometimes the connection drops and I can't seem to reconnect without a reboot, which was amounting to pulling the plug more often than I'd have liked.

I do have a USB mouse but it's prohibitive to click through menus to shut down the Pi through the GUI when headless.

My solution was to create a shell script with the contents sudo halt and give it execute permissions. My plan was to leave it in a specific corner of the desktop with no other icons and menu bar nearby so that I could target it for a double-click with the mouse blindly, without a monitor.

This strategy works nicely with two caveats:

  • By default, a dialog box pops up after double-clicking the script asking how to run the file. I disabled the dialog using

    file manager
      > edit
        > preferences
          > general
            > uncheck do not ask option on executable launch
    

    as described here and here.

  • The icon on the desktop isn't completely flush to the corner which makes it a bit harder to target, so I have to move my mouse until I'm sure it's in the extreme corner, then roll it back a little bit towards the middle before double-clicking.

    Practice on a monitor first, or when SSH'd in so you can tell for sure when you've hit the mark beyond simply looking at the green light activity indicator. Playing a sound before shutdown is another way to confirm success.

    For those with bad aim, I suppose you could fill the entire desktop with copies of the script or otherwise adapt this approach to fit your situation.

ggorlen
  • 105
  • 5
0

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

  • 2
    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 04:12
  • I am also running headless, but if this really works, I'd be glad to install the desktop environment. Then this is indeed a very simple method.Only caveat is that it would not work if the gui gets frozen – Roland May 05 '20 at 11:00
-1

from the GPIOZERO page. https://gpiozero.readthedocs.io/en/stable/recipes.html?highlight=shutdown#shutdown-button

This would support a physical button for shutdown.

Have you looked at application grade micro SD cards? It would have an A1 or A2 stamped on it. I've had to replace couple of sd cards with application grade due to the unit fading off and losing connection. Application grade is designed specifically for this type of use.

from gpiozero import Button
from subprocess import check_call
from signal import pause

def shutdown(): check_call(['sudo', 'poweroff'])

shutdown_btn = Button(17, hold_time=2) shutdown_btn.when_held = shutdown

pause()

peck
  • 109
  • 4