Python Script execution

Hi Guys,
i got myself an HDMIPI and i want to control the LCD Power Button via a script.
The Script looks like this:
############
#!/usr/bin/env python2.7
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN)

def toggle():
GPIO.setup(25, GPIO.OUT, initial=1)
GPIO.output(25, 0) # this is our simulated button press
sleep(0.2) # hold button for 0.2 seconds
GPIO.output(25, 1) # release button
GPIO.setup(25, GPIO.IN) # set port back to input (re-enables buttons)

print “finished, cleaning up”
GPIO.cleanup()
########################
So i’m looking for a possibility to trigger this python script during boot phase to switch the LCD on and again during shutdown phase to switch it off.

For the startup phase i read about to just name this script “autostart.py” and place it in the kodi folder /etc/osmc/.kodi.
Is this Information still valid for the current OSMC version 2016_08 ?
When i was looking for autostart.py and kodi i got some pages back talking about autoexec.py but also not sure if this is still valid.

For the shutdown phase i have found no idea so far.

Hopefully you can help me.
Thanks

Andreas

You could make it run on boot with crontab.
Lets say your script is located here: /home/pi/script.py

First install crontab
sudo apt-get install cron

If you want to run the scheduled task as root:
sudo crontab -e
otherwise for the osmc user just do:
crontab -e

At the bottom copy paste this (change accordingly with the location and name of your script):
@reboot sudo python /home/pi/script.py

And thats it. Everytime the pi boots, it will run that python script.

For shutdown, you could make a shortcut on your remote that when you press a button, it will execute your shutdown script. Hell, you could do the opposite as well, press a button to switch on the monitor.

I had some problems with this method before, which i explain here:

Somewhere in the middle of the page, i explain how to do it for python.

I am sure there is a simpler way to run scripts on boot/shutdown playing with rc.local, init.d etc, but crontab is much simpler.

If someone can post the ‘experts’ method showing that, it would be awesome.

You’ll want to save that script to the following path: ~/.kodi/userdata/autoexec.py
Then also make sure you change the mode to make the script executable.
Alternatively, I believe you can also use an rc.local script, too.

Hi Guys,
many thanks for the input.
I got the startup now working :slight_smile:
i have edit the /etc/rc.local and added the line
“sudo python /home/osmc/lcdtoggle.py”

That works perfect.

So i tried to play with rc0.d
I have copied the rc.local to /etc/init.d folder and renamed it lcdtoggle.sh
Then i created a link in rc0.d via “ln -s /etc/init.d/lcdtoggle.sh K01lcdtoggle.sh”

But this is not working when i “shutdown -h now” or use the shutdown button via the gui.

Are you sure /home/osmc is still mounted at the time you hit runlevel0
if not you will need to put your lcdtoggle.py into /etc/init.d, which you know is mounted.
Also, not sure if path still works in runlevel 0
you might need to specify the full path to python (/usr/bin/python) rather than just python
on the other hand it could be none of these things, but worth checking.

init startup the systemd way:

create systemd service file:

$ sudo su
# nano /lib/systemd/system/lcdtoggle.service

add to the file:

[Unit]
Description = GPIO LCD toggle

[Service]
Type = idle
ExecStart = /home/osmc/lcdtoggle.py

[Install]
WantedBy = multi-user.target

enable the service:

# systemctl enable lcdtoggle

start the service:

# systemctl start lcdtoggle

Hi,
thanks for the feedback.
So toggle the screen on is now working absolutely perfect, done either by the rc.local or the steps “rern” has suggested.

But i still getting the toggle off at shutdown not working. I have now placed everything in /etc/init.d as “pborman” has suggested. And also wrote the full path to python into my shellscript.

But still no luck. :frowning:

So if anyone has an idea how to run a shell or python script when a shutdown is triggered, please let me know.

Thanks
Andreas