35

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

8 Answers8

38

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,876
  • 1
  • 13
  • 23
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
  • 60,325
  • 17
  • 117
  • 234
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...

-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