From 8ad7b50ea071ef8050dc6b4de82b882ea145c366 Mon Sep 17 00:00:00 2001 From: Menno van der Graaf Date: Tue, 3 Aug 2021 00:56:59 +0200 Subject: [PATCH] Add powerbutton service --- services/powerbutton.py | 19 +++++++++++++++++++ services/powerbutton.service | 9 +++++++++ 2 files changed, 28 insertions(+) create mode 100644 services/powerbutton.py create mode 100644 services/powerbutton.service diff --git a/services/powerbutton.py b/services/powerbutton.py new file mode 100644 index 0000000..994c395 --- /dev/null +++ b/services/powerbutton.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python +""" +Shutdown script, that waits for a falling edge on a specified GPIO pin +""" +import os + +try: + from RPi import GPIO +except RuntimeError: + print("Error importing RPi.GPIO, need sudo?") +POWER_BUTTON_PIN = 27 +GPIO.setmode(GPIO.BCM) +GPIO.setup(POWER_BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) + +print('Monitoring shutdown button for falling edge') +while True: + GPIO.wait_for_edge(POWER_BUTTON_PIN, GPIO.FALLING) + print('Shutdown button pressed, shutting down, farewell') + os.system('systemctl poweroff') diff --git a/services/powerbutton.service b/services/powerbutton.service new file mode 100644 index 0000000..de74500 --- /dev/null +++ b/services/powerbutton.service @@ -0,0 +1,9 @@ +[Unit] +Description=Monitors powerbutton to send shutdown command + +[Service] +Restart=on-abnormal +ExecStart=/usr/local/bin/powerbutton.py + +[Install] +WantedBy=multi-user.target