Posts: 1
Joined: Thu Mar 22, 2018 12:12 am

Need help running a Python script at boot as pi user

Thu Mar 22, 2018 1:11 am

I have created a Python application which makes use of the sense_hat python library. When running as the default pi user the application works as expected. However, when invoked as the root user the application works, but the “electronic bubble” feature produces the wrong behavior.

Design notes;
  • Rpi3 running Stretch
  • The sense hat hardware (and rpi3) are rotated 90 degrees clockwise so that 8x8 grid is above the joystick and the USB/ether ports face the ground.
  • The “electronic bubble” function uses the sense_hat lib’s orientation functions to get pitch and roll (yes these are correct for my orientation) and displays on the 8x8 led grid an indication of “level”
  • Based on a lot of research and help from this site,, I determined that I need to change the AXISROTATION value in the /home/pi/.config/sense_hat/RTIMULib.ini file from a default of 0 to 5.
  • I’ve also made sure to copy this file to /etc/RTIMULib.ini.
  • AXISROTATION=5 is not optional, this is because the IMU at rest, with only the force applied by gravity can read a range of -1 to 1. The formulas which compute the angle make use of the ACOS function so the range in radians is 0 to Pi (mathematical pi 3.14……). As degrees that’s 0 to 180. So I can’t have the 0-180 axis in the same plan as my orientation, because the degrees values go from 178, 179, 180, 179, 178 and 2,1,0,1,2 as the device is rotated. In this case the system can’t tell if it's leaning left or right or front or back. AXISROTATION=5 rotates these axes out of the plane of interest so that I not only know the magnitude but also the direction of "unlevelness".
  • The core python script file is executed from the line “python3 /pathToMyScript/” which is in an file which is called from /etc/rc.local on boot.
My suspicion is that when the python script is ran as the pi user the RTIMULib diver is initialized with the /home/pi/.config/sense_hat/RTIMULib.ini config file. But, on boot the script is executed as the root user and neither of the ini config files are honored as the RTIMULib dirver initializes. Therefore, a default AXISROTATION of 0 is used and that throws my “electronic bubble” function completely off.

To confirm the above suspicion, I kill all python processes after boot. Then I enter the following in the command terminal, “sudo python3 /pathToMyScript/”. This produces that exact same faulty behavior as running from boot. To test the pi user, I enter “python3 /pathToMyScript/”, the system acts as expected.
My theory is that if the script can be executed at boot as the pi user then the system should work. As an attempt to fix this, I changed the line to “sudo -u pi python3 /pathToMyScript/”.

Posts: 5766
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Need help running a Python script at boot as pi user

Thu Mar 22, 2018 1:42 am

If you are booting to desktop then I would suggest that you try autostart instead of RC.local.

Code: Select all

Add to autostart:

Code: Select all

@python3 /pathToMyScript/
Or if your program needs terminal:

Code: Select all

@lxterminal -e python3 /pathToMyScript/
Doing this will start your program much later in the boot process or well after desktop is loaded by using sleep x command.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Stretch w/ Desktop OS.

Return to “Astro Pi”

Who is online

Users browsing this forum: No registered users and 3 guests