Auto startup as a systemd service
(Need only a push button.)
1.Install RPi.GPIO if not yet.
$ sudo su
# apt-get update
# apt-get install python-pip python-dev gcc
# pip install rpi.gpio
2.Create a python script for push button shutdown:
# nano /home/osmc/shutdown.py
Add to the file:
#!/usr/bin/python
import RPi.GPIO as GPIO
import os
# hide warning
GPIO.setwarnings(0)
# connect RPi numbered pin
button = 33
# hide warning
GPIO.setwarnings(0)
# pins setup
GPIO.setmode(GPIO.BOARD)
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# wait for button press
print('Wait for button press...')
GPIO.wait_for_edge(button, GPIO.FALLING)
# activate system command
os.system('sudo shutdown -h now')
Change permission:
# chmod +x /home/osmc/shutdown.py
3.Create a systemd service file:
# nano /lib/systemd/system/shutdown.service
Add to the service file:
[Unit]
Description = GPIO shutdown button
[Service]
Type = idle
ExecStart = /home/osmc/shutdown.py
[Install]
WantedBy = multi-user.target
4.Enable auto startup:
# systemctl enable shutdown
Connect a push button to RPi GPIO pin #33 + #34
Start the service without reboot:
# systemctl start shutdown
Press the button to shutdown.
Easier Alternative:
If you already have a working remote control, it’s easier to map a button to shutdown command.
Create a python script for shutdown:
$ sudo su
# nano /home/osmc/shutdown.py
Add to the script file:
#!/usr/bin/python
import os
os.system('sudo shutdown -h now')
Create a custom keymaps file:
# nano /home/osmc/.kodi/userdata/keymaps/remote.xml
Add to the keymap file:
(‘zero’ can be change to any button)
<?xml version="1.0" encoding="UTF-8"?>
<keymap>
<global>
<remote>
<zero>RunScript(/home/osmc/shutdown.py)</zero>
</remote>
</global>
</keymap>
Restart (Power > Exit) to load the custom keymaps file.
Press the remote button to shutdown.