Trying to add off button with script at startup, not working

hi, I am completely new to osmc and the linux language. I have a raspberry pi Model A. I added the pidrive (314 Gb harddrive) to it and copied movies onto it so my kids can watch them in the car ( screens are built into the head rest). The pi mediacenter works great as it is, but i have to get out and shut it down thru the onscreen menus before i turn off the car (pi and pidrive get power from car 120v outlet in car). What i want to do is add a push button to the pi and have it shut itself off (easier and faster for my kids to deal with). I found a product that does this,

the RemotePi ( http://www.msldigital.com/ ).

they provide instructions on how to add a line of code to the rc.local that calls another shell script that monitors the button and triggers a shutdown sequence.

just what i am looking for. I don’t need the IR receiver hardware, i just need to add a button and to get this script to work. Problem is, i can’t get it to work. I have spent several days researching how the different parts work and have tried debugging it with no success. at this point i am just trying to verify that the rc.local is able to call another script that creates a txt file telling me that part works, but i can’t even get that to work. Here is what my rc.local looks like:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# original code from RemotePi
# (/etc/irswitch.sh)&

#debug code i am working with
sudo /etc/samplerun.sh

exit 0

and my testing script is here samplerun.sh:

#!/bin/bash

sudo echo "new test" > testfile2.txt

exit 0

I use PuTTy on my windows desktop to access the pi and when osmc is running i can run rc.local just fine, it creates the txt file correctly but when i reboot it, no file is created.

I am at a loss for what to do next, any help would be appreciated.
thx, Michael

sudo echo probably won’t work. Use

echo "...." | sudo tee testfile2.txt instead.

As an aside, thanks for the suggestion. I had not considered a need to power down OSMC from a menu other than the home screen. I’ll add support for powering off the device to our remote’s long press functionality.

I also suggest you look in to using a systemd unit, instead of adding code to /etc/rc.local.

Sam

For proper shutdown when you switch of the car it might be more recommended to look at one of the Pi UPS.

no luck, did not work,

I changed the echo line in the samplerun.sh and it did not create the desired testfile2.txt after restart. but did work when i ran rc.local manually.

also, i don’t know how to work with systemd, i looked into it but was unable to find enough info for me to use it.

If you are always running your script from /etc/rc.local, you don’t need sudo, as it’s always run as a root user.

Check that /etc/samplerun.sh has the execute bit (chmod +x) set.

Also note: it appears the original author starts it in a backgrounded subshell. This suggests to me that whatever you’re running is a long-term process. You should do the same, or it will hang. Further more – if the service dies, it will not be restarted as it is not being managed by init.

We have an early Wiki on running things at startup: Running scripts on startup and shutdown

Sam

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.

1 Like

THANK YOU SO MUCH FOR THIS!! i have re-used this code and updated it for the NESPi+ Case and OSMC and button support. I named my updated script after you too. :wink: