Question

Good Day All,

I am trying to run a python script at boot using Rasbian on a Raspberry PI.

I have added the following into crontabt -e:

   @reboot /usr/bin/python /home/pi/Midori_Monitor/Main_Midori.py

That did not work. I then tried adding it to rc.local:

   # Print the IP address
   _IP=$(hostname -I) || true
    if [ "$_IP" ]; then
    printf "My IP address is %s\n" "$_IP"
    fi

    /usr/bin/python /home/pi/Midori_Monitor/Main_Midori.py

    exit 0

That did not seem to work either. I tried adding the user and group (pi:pi) to both and that did not help.

I don't see a log for the crontab however syslog offers:

    Feb 21 13:44:57 raspberrypi /USR/SBIN/CRON[1991]: (pi) CMD (pi:pi /usr/bin/python       /home/pi/Midori_Monitor/Main_Midori.py)
    Feb 21 13:48:22 raspberrypi /USR/SBIN/CRON[1964]: (pi) CMD (pi:pi /usr/bin/python   /home/pi/Midori_Monitor/Main_Midori.py)
    Feb 21 13:48:24 raspberrypi /USR/SBIN/CRON[2008]: (pi) CMD (pi:pi /usr/bin/python /home/pi/Midori_Monitor/Main_Midori.py)
    Feb 24 07:22:18 raspberrypi /USR/SBIN/CRON[1983]: (pi) CMD (/usr/bin/python /home/pi/Midori_Monitor/Main_Midori.py & )
    Feb 24 07:28:13 raspberrypi /USR/SBIN/CRON[1983]: (pi) CMD (/usr/bin/python /home/pi/Midori_Monitor/Main_Midori.py & )
    Feb 24 07:34:04 raspberrypi /USR/SBIN/CRON[1993]: (pi) CMD (/usr/bin/python /home/pi/Midori_Monitor/Main_Midori.py  )

Any suggestions?

Was it helpful?

Solution

SOLVED - I think the core being here that my script is required to run in a GUI environment.

I found this solution here: https://raspberrypi.stackexchange.com/questions/8734/execute-script-on-start-up

If you want a script to run when you boot into the LXDE environment, you could take a look at this Raspberry Pi forum post:

Navigate to:

  etc/xdg/lxsession/LXDE

Open the autostart file in that folder:

 sudo nano autostart

Add

 @python /path/to/script on a new line. 

If you want to run something like a python script, put something like @python mypython.py on a new line. Running a script file would be @./superscript, but for some reason the script runs in an infinite loop (perhaps this will stop that).

Save and exit:

 Ctrl+X, Y, Enter

Restart your Raspberry Pi into the LXDE environment.

OTHER TIPS

You added the code after exit 0, so it's never executed!

Use this rc.local:

 # Print the IP address
 _IP=$(hostname -I) || true
 if [ "$_IP" ]; then
   printf "My IP address is %s\n" "$_IP"
 fi

 /usr/bin/python /home/pi/Midori_Monitor/Main_Midori.py

 exit 0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top