datanaut
Posts: 11
Joined: Mon Jan 21, 2013 10:05 pm
Location: London W9

rc.local GPIO python script won't run/start

Mon Jan 21, 2013 11:46 pm

I can't get the python script to run from rc.local at startup.
The scrip runs fine from the command line
Other scripts (not python - New to python and picked this up) and they work fine
What am I missing?
Where can I look for errors?
What can I add to try and de-bug?

Thanks .....

In rc.local I've tried

python /home/pi/relay/bootGPIO.py &
and
/home/pi/relay/bootGPIO.py &

Code: Select all

#!/bin/sh -e
# rc.local
# Fire up the GPIO scripy 
/home/pi/relay/bootGPIO.py &
exit 0
In the script I've tried

#!/usr/bin/env python
and
#!/usr/bin/python

Code: Select all

#!/usr/bin/python

from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
def switchState(switch,state):
	#print "Sw {0} sec {1}" .format(switch,state) 
	if state > 0:
		GPIO.output(switch, GPIO.LOW)
	else:
		GPIO.output(switch, GPIO.HIGH)

switchState(7,1)
sleep(1)
switchState(7,0)
sleep(1)
switchState(7,1)
sleep(1)
switchState(7,0)

User avatar
bfagioli
Posts: 123
Joined: Sat Oct 27, 2012 11:49 pm

Re: rc.local GPIO python script won't run/start

Tue Jan 22, 2013 12:58 am

Are you sure rc.local is running.

The rc.local on my system has this comment:

Code: Select all

# In order to enable or disable this script just change the execution
# bits.
Make sure its executable. Could it be that simple? Maybe :)

datanaut
Posts: 11
Joined: Mon Jan 21, 2013 10:05 pm
Location: London W9

Re: rc.local GPIO python script won't run/start

Tue Jan 22, 2013 1:18 am

bfagioli wrote:Are you sure rc.local is running.

The rc.local on my system has this comment:

Code: Select all

# In order to enable or disable this script just change the execution
# bits.
Make sure its executable. Could it be that simple? Maybe :)
The permissions and correct. I have other scripts in rc.local that work fine. I've only started using python in the last few days. I feel it might be something like the GPIO library not loading or something like that. My python script works fine from the command line. Thanks for your input :-)

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: rc.local GPIO python script won't run/start

Tue Jan 22, 2013 2:10 am

GPIO python scripts must be run as root. Does rc.local run as root?
There are 10 types of people in this world. Those that understand binary, and those that don't.

datanaut
Posts: 11
Joined: Mon Jan 21, 2013 10:05 pm
Location: London W9

Re: rc.local GPIO python script won't run/start

Tue Jan 22, 2013 3:42 am

rickseiden wrote:GPIO python scripts must be run as root. Does rc.local run as root?
Yes. The /etc/rc.local runs as the last part of the boot process before login becomes available. rc.local is owned by root and the python script it calls is executable by root as well. The rc.local also has the full path to the python script. Thanks for your input :-)

User avatar
bfagioli
Posts: 123
Joined: Sat Oct 27, 2012 11:49 pm

Re: rc.local GPIO python script won't run/start

Tue Jan 22, 2013 4:20 am

I used your python code and it worked for me. The only difference was I used GPIO PIN 11 instead of 7 but that shouldn't matter. One thing that surprised me was that the code did no execute until after LXDE was upand running and then a few seconds later I saw the CPU go to 100% and then the LED tuned on and off a few times. At that point the script ends and the CPU goes back to 0%. I would have thought that the rc.local wold have run well before that.

I also tried some C# code which also blinks the LED on pin 11. Its running in the Mono CLI and launched from rc.local also and that also worked fine.

Not sure why its not working for you.

datanaut
Posts: 11
Joined: Mon Jan 21, 2013 10:05 pm
Location: London W9

SOLVED rc.local GPIO python script won't run/start

Tue Jan 22, 2013 2:31 pm

SOLVED
I added thease two lines of code below to the top of the rc.local file and this showed that my script was executing OK.

Code: Select all

exec 2> /tmp/rc.local.debug
set -x
I then looked for anything else that might affect the GPIO.
I have WebIOpi ( http://code.google.com/p/webiopi/ ) installed. WebIOpi is started in the /etc/init.d/ folder. When I removed webiopi from that folder my script ran :-)

There must be some conflict between WebIOpi and my python code. I don't know what it is but I will investigate further as I want to use a browser interface to control the GPIO across the internet as this is part of a home automation project.

Thanks to all who helped.

Gergmchairy
Posts: 31
Joined: Thu Aug 30, 2012 3:22 pm

Re: rc.local GPIO python script won't run/start

Sat Jul 06, 2013 9:53 am

I realise this in an old post, but I have just had the same problem - and if anyone else stumbles across the thread this may help!

It seems that calling the python module urllib2 too early caused the problem for me, so i inserted time.sleep(30) at the front of my script (after the shebang & imports etc) and it runs fine now!

HTH Greg.

Abgas-Engel
Posts: 1
Joined: Thu Jan 11, 2018 2:46 pm

Re: rc.local GPIO python script won't run/start

Thu Jan 11, 2018 3:02 pm

Thanks a lot!!
A crazy behavior of my RasPi/Python came to an end. The GPIO-Script didn't start until I added the time.sleep(30). Very simple.

Return to “Troubleshooting”