• Stars
    star
    125
  • Rank 286,335 (Top 6 %)
  • Language
    Shell
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python daemon to watch a GPIO pin and trigger a clean shutdown.

clean-shutdown

clean-shutdown is a simple daemon that monitors a user-specified GPIO pin and triggers a clean software shutdown when that pin is asserted low. It offers ways to customise the behaviour of the shutdown process to fit most use-cases.

Installation

The setup.sh script provided in this repository can be used to set your preference of GPIO pin to monitor in order to initiate the shutdown. To install support for a specific product however, such as OnOff SHIM or Zero LiPo, we recommend you use the one-line installers listed further down the page.

We highly recommend you use the generic one-line installer rather than run the setup.sh script directly, like so:

curl https://get.pimoroni.com/cleanshutdown | bash

If you need to however, for example because the above command states that your operating system is not supported, clone this repository locally and run setup.sh. When prompted, enter the pin you would you like to use as trigger for the shutdown.

Note that the setup script expects an integer value between 4 and 27 (you can use others outside this range by manually editing the config file as explained below, but there are caveats so if it does not quite work, you're on your own!)

Supported Products

In addition to the generic install described above, where you get to choose your preferred custom trigger pin, the following specific products are supported:

  • OnOff SHIM - Uses BCM 17 as trigger and LED pin, and BCM 4 as the power off pin; argument to pass: onoffshim
  • Zero LiPo / LiPo SHIM - Uses BCM 4 as the power off pin; argument to pass: zerolipo

You may pass the setup.sh the product as an argument, as detailed above. HOWEVER, again, we recommend that you use our one-line installers, which are better equipped to handle some specific distributions idiosyncracies.

OnOff SHIM

If you are using a OnOff SHIM, you should use our dedicated one-line installer, which will ensure the daemon configuration is optimal for that product:

curl https://get.pimoroni.com/onoffshim | bash

Turning power off/on using second RPi

In some circumstances it could be neccessary to power cycle one RPi using another RPi.

  • For example if a remote RPi host hangs (failing predefined tests like ping) and therefore must to be power cycled.
  • Or in a High-Availability setup where there are two RPis hosts which should be able to cut off power to eachother acting as a STONITH device.

OnOffShim can be used as such device. In order to do that you need to connect OnOffShim to both RPis. Here is a diagram which shows the first setup from above examples where RPi Zero is acting as a "master" host that turns power off and on to a "slave" RPi3 host.

img After you have installed the software using one-liner mentioned above you can use these two commands to mannually turn power off and on to "slave" RPi

Turn Off power

#sudo raspi-gpio set 4 op dl

Turn On power

#sudo raspi-gpio set 4 op dh

Keep in mind that cutting power off to RPi can corrupt your SD card. Use Backups or make filesystem Read-Only.

Zero LiPo / LiPo SHIM

If you are using a Zero LiPo / LiPo SHIM, you should use our dedicated one-line installer, which will ensure the daemon configuration is optimal for that product:

curl https://get.pimoroni.com/zerolipo | bash

Usage

There is really not a lot that needs to be done once the daemon is in place - which will be the case after installation and reboot... this is all very straightforward!

That said, clean-shutdown has some interesting features that your particular use-case may require. For example, if you are using an input trigger you would normally expect the shutdown to occur as soon as you press the control. But what if that is not what you want?

clean-shutdown provides several useful parameters to adapt the shutdown behaviour or exact monitoring environment to your project, without requiring you to mess with the daemon code, or understand what it does in the finer details.

If you find yourself in such a need, fire up your favourite editor and open /etc/cleanshutd.conf. The parameters documented below can then be customised as desired.

Note that in order for parameters changes to take effect the deamon has to be restarted. The easier way to do that is to reboot the Pi, or run:

sudo service cleanshutd restart

Parameters

daemon_active

This is a pretty hacky way to passify the daemon without needing to delve into the details of systemd. Set to 0 to deactivate the daemon (technically the daemon will be started at boot time but it will do absolutely nothing). Set to 1 to reactivate.

trigger_pin

Normally you'll set this up at install time and won't need to change it, but... as we do, next week you might buy a nice shiny (Pimoroni) HAT or pHAT and find that the pin you had your clean shutdown trigger wired to is required by your new friend. Just move the trigger button to another pin and reboot! The unit used for this parameter is the bcm number for the pin (4 or above recommended, 0-3 have particularities that make them slightly less straightforward to use, though the daemon will happily monitor them for you, so as long as you know what you're doing go right ahead).

led_pin

This parameter determines a pin can be pulled low to blink a status LED, first showing that shutdown has been armed, and finally blinking three times to show that final power off is imminent. So feel free to wire a LED onto a GPIO pin and set the parameter accordingly if you like.

poweroff_pin

For products that support it (eg: OnOff SHIM), the poweroff_pin determines which pin will be pulled low right at the end of your Pi's shutdown process. If supported, this will cause power to your Pi to be cut completely.

hold_time

This parameter determines the amount of time you must hold down the button until shutdown occurs. It defaults to 1 second to avoid accidental shutdowns. The unit for hold_time is expressed in seconds. Use 0 to shutdown as soon as the button is pressed.

shutdown_delay

Most of the time you probably want your Pi to shutdown as soon as the trigger occurs, but sometimmes, like with the Zero Lipo, once the battery warning has been detected you still got some life of the LiPo before it is necessary to shut it down (the 'battery low' warning is activated at 3.4V, but the protection circuitry will only cut off the supply at 3.0V). The unit for shutdown_delay is expressed in minutes (0, the default, means immediate shutdown).

polling_rate

This parameter determines how often the trigger is checked for. Normally, a small but reasonable value, say a second or 2 is adequate to detect a button press without polling constantly, but if you take the Zero Lipo example again it really does not matter if the monitoring is more relaxed, say if polling is performed every 30 seconds or so. There may be other use-cases where smaller or larger values are optimal, so there's a parameter for the occasion if you find yourself in one. Units for polling_rate are expressed in seconds.

Parasitic Shutdowns

Be aware that altering the state of trigger_pin can throw you in a scenario where your Pi shuts down right away upon boot, if a process, dtoverlay, or HAT EEPROM, just to name a few possibilities, pulls it low on boot (or set it as an output, which implies it being driven low initially).

If this occurs right after you plugged a HAT, then try booting without it attached, and disable the cleatshutd service with:

sudo systemctl disable cleanshutd

There is another way, which is provided as an emergency solution for scenarios where reaching the bash prompt is not possible (because the Pi shuts down before you get a chance to do so).

In such cases, or as an alternative to the above, you may add the following to your /boot/config.txt file from another computer:

disable_cleanshutd=1

More Repositories

1

pimoroni-pico

Libraries and examples to support Pimoroni Pico add-ons in C++ and MicroPython.
C
1,249
star
2

inky

Combined library for V2/V3 Inky pHAT and Inky wHAT.
Python
569
star
3

enviroplus-python

Python library for the Enviro+ environmental monitoring board
Shell
383
star
4

unicorn-hat

Python library for Unicorn pHAT and HAT. 32 or 64 blinding ws2812 pixels for your Raspberry Pi
C
363
star
5

hyperpixel4

Driver for the Pimoroni HyperPixel 4.0" Touchscreen Display
Shell
322
star
6

blinkt

Python Library for Blinkt; 8 APA102 LEDs for your Raspberry Pi
C
304
star
7

fanshim-python

Python library for the Fan SHIM for Raspberry Pi
Python
294
star
8

bme680-python

Python library for the BME680 gas, temperature, humidity and pressure sensor.
Python
261
star
9

pirate-audio

Examples and documentation for the Pirate Audio range of Raspberry Pi add-ons
Python
239
star
10

phew

Python
204
star
11

st7789-python

Python library to control an ST7789 240x240 1.3" TFT LCD display.
Python
193
star
12

keybow-firmware

Keybow Firmware for the Raspberry Pi Zero
C
180
star
13

unicorn-hat-hd

Python library and examples for Unicorn HAT HD
Python
173
star
14

scroll-phat-hd

Python library for Scroll pHAT HD
Python
159
star
15

explorer-hat

Python library for Explorer HAT
Python
158
star
16

badger2040

Examples and firmware for Badger 2040 and Badger 2040 W
Python
154
star
17

displayotron

Libraries and examples for the Display-o-Tron 3000 and Display-o-Tron HAT
Python
145
star
18

picosystem

PicoSystem libraries and examples.
C++
136
star
19

piglow

Python library & examples for the Pimoroni PiGlow
Python
133
star
20

inky-phat

Python library for Inky pHAT
Python
126
star
21

automation-hat

Python library and examples for the Pimoroni Automation HAT, pHAT and HAT Mini
Python
116
star
22

enviro

Python
108
star
23

enviro-phat

Python libraries and examples for the Pimoroni Enviro pHAT
Python
106
star
24

scroll-phat

Library and examples for Scroll pHAT. 11x5 LEDs of goodness!
Python
103
star
25

pantilt-hat

Python library for the Pimoroni PanTilt servo and lighting HAT
Python
102
star
26

phat-beat

Python library for PHAT BEAT - A stereo DAC, AMP and VU for the Raspberry Pi
Python
99
star
27

hyperpixel

Setup for the Pimoroni Hyper Pixel 800x480 pixel multi-touch display
Python
96
star
28

vl53l1x-python

Python library for the VL53L1X Laser Ranger
C
91
star
29

pmk-circuitpython

Python
88
star
30

skywriter-hat

Skywriter python libraries and examples
Python
87
star
31

pico-boilerplate

A boilerplate C++, CMake project for the Raspberry Pi Pico.
CMake
79
star
32

rainbow-hat

Python library and examples for the Pimoroni Rainbow HAT
Python
73
star
33

pibrella

Pibrella python library & examples
Python
71
star
34

breakout-garden

Documentation, software, and examples for the Breakout Garden ecosystem.
Shell
70
star
35

keybow2040-circuitpython

CircuitPython library for the Pimoroni Keybow 2040
Python
68
star
36

bme280-python

Python library for the BME280 temperature, pressure and humidity sensor
Python
63
star
37

picade-hat

Picade HAT input daemon and setup script
Shell
60
star
38

pivumeter

ALSA plugin for displaying VU meters on various Raspberry Pi add-ons - derived from ameter
Shell
59
star
39

grow-python

Python libs for controlling the Grow HATs
Python
56
star
40

adxl345-python

Python library for the ADXL345
Python
55
star
41

bmp280-python

Python library for the BMP280 temperature, pressure, and altitude sensor.
Python
55
star
42

python-multitouch

Multi-touch pure Python driver for the official 7" touchscreen display.
Python
55
star
43

Piano-HAT

Python library and examples for Piano HAT Raspberry Pi Add-on board
Python
53
star
44

speaker-phat

Software installer for Speaker pHAT
Python
51
star
45

pms5003-python

Python library for the PMS5003 particulate sensor
Shell
50
star
46

microdot-phat

Python Library and Examples for Micro Dot pHAT. A 6-matrix driver for retro 5x7 displays!
Python
49
star
47

icm20948-python

Python library for the Pimoroni ICM20948 breakout
Shell
48
star
48

pidi-spotify

Python
46
star
49

picovision

C++
46
star
50

PanTiltFacetracker

Face Tracking for the Adafruit Mini Pan Tilt kit and Raspberry Pi camera
Python
44
star
51

unicornhatmini-python

Python library for the Pimoroni Unicorn HAT Mini RGB LED Display
Python
42
star
52

Picade-Sketch

Picade Arduino sketch
C++
39
star
53

eagle

Custom Eagle parts that we've created where needed. Please feel free to use, extend, and share! (CC v3.0 Share-Alike You are welcome to use this library for commercial purposes.)
39
star
54

trackball-python

Python library for the Pimoroni Track Ball Breakout
Python
36
star
55

sgp30-python

Python library for the SGP30 air quality sensor
Shell
35
star
56

button-shim

Python library and examples for the Pimoroni Button SHIM
Python
33
star
57

keybow-python

Python library for the Pimoroni Keybow 3 and 12 key mechanical keyboard add-ons for Raspberry Pi
Python
32
star
58

led-shim

Python library for the Pimoroni LED SHIM
Python
31
star
59

pimoroni-pico-rp2350

MicroPython builds for Pimoroni Pico 2 / RP2350 boards.
Python
30
star
60

hyperpixel2r

Shell
30
star
61

picade

All Picade related stuff.
C
30
star
62

displayhatmini-python

Python library for the Pimoroni Display HAT Mini
Shell
30
star
63

gfx-hat

Python library for the Pimoroni GFX HAT
Python
29
star
64

fourletter-phat

Python library for the Pimoroni Four Letter pHAT
Python
28
star
65

drum-hat

Python library for the Pimoroni Drum HAT Raspberry Pi Add-on board.
Python
27
star
66

pmw3901-python

Python library for the PWM3901 optical flow-rate sensor
Python
26
star
67

rp_usbdisplay

Binary drivers for the RoboPeak/DFRobot 2.8 USB TFT, version 2
C
25
star
68

i2cdevice-python

Domain-specific language for describing smbus/i2c register maps
Python
24
star
69

flotilla-python

Python library for the Pimoroni Flotilla plug-and-play electronics ecosystem
Python
24
star
70

mote

Mote - drivers for the plug and play USB APA102 controller
Python
24
star
71

PiratePython

PiratePython: a minimal Raspbian for Python programmers
C
24
star
72

hyperpixel2r-python

Shell
23
star
73

propeller-hat

Propeller HAT library code, examples, documentation and SPIN source
Propeller Spin
23
star
74

weatherhat-python

Python
22
star
75

trilobot-python

Python
21
star
76

mopidy-raspberry-gpio

Mopidy GPIO Control Plugin for the Raspberry Pi (Using RPi.GPIO)
Python
21
star
77

pico-circuitpython-examples

CircuitPython examples for Pimoroni RP2040 boards.
Python
20
star
78

internet-of-seeds

The code that runs our IKEA VÄXER Internet of Seeds
Python
20
star
79

max30105-python

Python library for the Pimoroni MAX30105 breakout
Python
19
star
80

as7262-python

Python library for the as7262 spectral sensor breakout
Python
17
star
81

node-red-nodes

Node-RED nodes for Pimoroni products
JavaScript
17
star
82

Raspberry-Pi-Web-RGB

Simple Raspberry Pi RGB LED control from your Web Browser using Python and Flask
HTML
17
star
83

plasma

Plasma LED driver, FX Sequencer and plugins
Python
16
star
84

fonts-python

Python font packaging framework
Python
15
star
85

vl53l5cx-python

CTypes Python bindings for the VL53L5CX
C
15
star
86

yukon

Python
15
star
87

pa1010d-python

Python library for the Pimoroni PA1010D GPS Breakout
Shell
15
star
88

ltr559-python

Python library for the LTR-559 optical presence/proximity sensor
Python
14
star
89

programming-trinket-with-raspberry-pi

Programming Adafruit's Trinket (3v3) directly from a Raspberry Pi
C
13
star
90

drv2605-python

Python library for the Pimoroni Haptic Vibes DRV2605 breakout
Python
12
star
91

touch-phat

Python library and examples for Pimoroni Touch pHAT - six touch sensitive buttons to control your project.
Python
12
star
92

mote-phat

Drive four channels of APA102 pixels from your Pi Zero with Pimoroni Mote pHAT
Python
12
star
93

rgbmatrix5x5-python

Python library for the Pimoroni 5x5 RGB Matrix Breakout
Python
12
star
94

pimoroni-pico-stubs

Python
11
star
95

pms5003-micropython

Python
11
star
96

pidi-plugins

🎵 plugins for Pirate Display. A mopidy/mpd album art downloader/displayer.
Python
11
star
97

mopidy-pidi

Mopidy plugin host for pidi display plugins.
Python
11
star
98

sn3218

Python library for the SN3218 18-channel LED driver
Python
10
star
99

bh1745-python

Python library for the bh1745 colour sensor breakout
Python
10
star
100

EnviroPlus-FeatherWing

CircuitPython library for the Enviro+ FeatherWing
Python
10
star