# Failure Leads to Growth

Since I purchased the Raspberry Pi, I have managed to kill two MicroSD cards.

The Raspberry Pi always stops responding at unexpected times, throwing a frustrating No route to host error when trying to SSH into it. When I angrily unplug the power, preparing to reboot, I find that it won’t boot up again - the file system is damaged. Following methods found online to fix the storage media, using the fsck command was fruitless. After formatting, when I painstakingly reinstalled all the software, ready to reboot, I encountered disk failures again, making a restart impossible - that card was a goner.

# Properly Restarting the Raspberry Pi

It’s not about the cost of an SD card, but the time it takes to reconfigure a new system after re-flashing it. Until a foolproof system backup method is found, safely restarting the Raspberry Pi, especially when the system is unresponsive, becomes crucial.

What is the proper shutdown procedure? The main issue lies in the fact that background processes within the system continuously access data, especially during tasks like downloading with Aria2. Pulling the plug directly can cause data inconsistencies between memory and the disk, potentially leading to data corruption from incomplete write operations.

A proper restart involves signaling all applications to stop running, cease writing to the file system, and synchronize any pending operations with the file system. To achieve this on an unresponsive Unix system, an evident way is to have a daemon run the terminal command for a restart or utilize the SysRq command at the Kernel level.

# Using Terminal Commands

The simplest shutdown commands are sudo shutdown -h now and sudo poweroff, while the straightforward reboot command is sudo reboot. These commands are safe, meaning they won’t damage the file system.

To use these commands when the system is unresponsive, the idea is to establish a background program that listens for specific input triggers to automatically execute the aforementioned commands.

Clever Earthlings have invented a series of solutions based on this principle, including:

I chose the third solution because… no need to acquire any new equipment!

It’s very convenient to use. First, clone the git repository git clone https://github.com/adafruit/Adafruit-GPIO-Halt and then compile it:

cd Adafruit-GPIO-Halt
make
sudo make install

Create a new systemd service.

sudo nano /lib/systemd/system/gpio-halt.service

Edit the service file with the following content.

[Unit]
Description=Short pins 21 and ground to shutdown the Pi
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/local/bin/gpio-halt 21 &

[Install]
WantedBy=multi-user.target

Start the service with these commands.

sudo systemctl daemon-reload
sudo systemctl enable gpio-halt.service
sudo systemctl start gpio-halt.service
sudo systemctl status gpio-halt.service # Check running status

If your Pi has 26 pins, short GPIO7 and GND; if it’s a 40-pin Pi 4B like mine, short GPIO21 and GND. Just tap it with a small key, and you can easily and safely restart the Raspberry Pi!

Pi GPIO Pins

If you are using the temperature control system I shared, remember to set the temperature control pin to a GPIO other than GPIO21, or the Pi will restart when the fan stops.

# Using SysRq

Another solution is to use the magical SysRq key (Magic SysRq Keys), a set of key combinations that the Linux kernel understands to help users perform a series of low-level operations, such as shutting down all services or rebooting. The specific keys are Alt+SysRq/PrintScr+function key, and the function key must be pressed without releasing the other two keys.

There are two prerequisites for using SysRq:

  1. The Kernel is still running
  2. A keyboard is connected

A common use case for SysRq is to safely restart an unresponsive Unix system. This sequence of key combinations is known as REISUB, where these six function keys are pressed in order to achieve a safe restart.

  • unRaw, regains keyboard control from X (graphical interface)
  • tErminate, sends SIGTERM to all processes to safely stop them
  • kIll, sends SIGKILL to all processes except init, forcefully terminating processes not yet closed
  • Sync, synchronizes memory data with the file system
  • Unmount, safely unmounts the file system
  • reBoot, restarts the system

By sequentially pressing the key combinations while the keyboard is connected, a safe restart can also be achieved.

# Data is Priceless

Data is priceless, so cherish it.