I based this project off of Shutdown Raspberry Pi with a Single Button. I used the scripted approach only so there is no reference here to using the Boot Overlay method.
This project consists of:
- Single Python script
- SYSTEMD Service definition file
- Single Button
- Single LED
I built this for a Raspberry PI Application that would be running unattended 24x7. Since I was not always available, I wanted to be able to instuct someone else to reboot the PI if needed. I added the light to provide feedback. The user instructions are:
- Find the flashing light with the button next to it
- Push and hold the button. The button will stop flashing and stay on. If you hold it long enough the light will go out.
- Release the button
- Wait a minute until you see the light flashing again.
- If the light does not come back flashing, call me.
You can review the Python code to see how the program works.
Some notes:
- This was developed on a Raspberry PI P400 running Raspbian GNU/Linux 11 (bullseye)
- The blinking LED runs independantly once started.
- The pins for the button and LED were chosen based on the requirements of the overall project.
- You will see print statements in the Python script. These are written to a log file defined in the services definition. This is optional if you don't need them.
- I set the output, error and working directory to an attached /media disk since I will be running the PI OS in read only mode. These can also be elminated if not needed.
- I wasn't able to find anyway to comment the services definition file.
You can find more details on using the SYSTEM service definition details here A good source for understanding PI Services is The ultimate guide on using systemd to autostart scripts on the Raspberry Pi
The following copies the files to their execution directories, reload the services, sets the reboot_button service to automatically run and starts it for the first time
sudo cp reboot_button.py /usr/bin
sudo cp reboot_button.service /lib/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable reboot_button
sudo systemctl start reboot_button
You can check on the status of the service. Here is an example
sudo systemctl status reboot_button
● reboot_button.service - Reboot Button Service
Loaded: loaded (/lib/systemd/system/reboot_button.service; enabled; vendor>
Active: active (running) since Sun 2022-03-06 16:08:29 EST; 1h 25min ago
Main PID: 2049 (python3)
Tasks: 4 (limit: 4915)
CPU: 6.888s
CGroup: /system.slice/reboot_button.service
└─2049 /usr/bin/python3 -u /usr/bin/reboot_button.py
Mar 06 16:08:29 paulsPI systemd[1]: Started Reboot Button Service.