0

I am trying to make the raspberry run my python script as soon as there is power supply. I have found some ways is to prepaire the rc.local file I did and i tried to execute the rc.local file alone it works but it doesn't work when I just plug in the power supply, nor when open the puTTY session even not when I open the lxsession (It doesnt run automatically in any of the cases) my rc.local

        #!/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.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
sudo python /home/pi/t3.py  --cascade=/home/pi/s.xml 0 
exit 0
2
  • 1
    Firstly the sudo is unnecessary (and does nothing as rc.local is running as root). Remove this and give it a try. It is also possible that something in the Python script is not running; probably because you are not logged in, or paths are not complete.
    – Milliways
    Commented May 20, 2014 at 4:30
  • 1
    as a standard user run "which python" and then use that full path to the python executable. You can also add " 2>&1 /tmp/t3.log" to the end of the line and see what the command generated.
    – rob
    Commented May 20, 2014 at 9:27

4 Answers 4

2

this command needs a few changes to be run perfectly:

sudo python /home/pi/t3.py  --cascade=/home/pi/s.xml 0
  1. get rid of sudo, rc.local is already run by superuser
  2. put the complete paths there (environment might be not as you remember it)
  3. add & at the end of command, because rc.local is supposed to finish fast
  4. redirect outputs so your script does not get stuck when buffer is full

finally your command becomes something like:

/usr/bin/python /home/pi/t3.py --cascade=/home/pi/s.xml 0 >/dev/null &
0

You can use 'su' command instead 'sudo'. Your command should look like below. Assume that your username is 'pi'.

su pi -c '/usr/bin/python /home/pi/t3.py  --cascade=/home/pi/s.xml 0'
0

You can use cron to run a script on boot of the rpi

crontab -e

Add to the last line of the file

@reboot python /home/pi/t3.py --cascade=/home/pi/s.xml 0

From this website

2
  • I have tried to do this the line I add is write has no problems because I tried to run it from the terminal direct it does, I just added @reboot at the beginning but It still doesn't run auto! any idea what is the problem Commented May 23, 2014 at 2:08
  • Can you show us the contents of your cron table?
    – mrwhale
    Commented May 24, 2014 at 0:20
0

rc.local executes as the final stage of boot. You need to wait until it would normally ask for your login credentials if a screen was plugged in. At the start of boot, nothing is ready, booting is setting everything up. You could made the boot time faster, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.