Today I made a new fresh install of Raspbian-Stretch on my old RaspberryPi 1B. That board mainly has some python programs on it that MQTT info onto my network to OpenHab. The previous install had become a bit broken I think, it would crash every two days and rather than sorting that out, I decided a fresh install would be the quickest.
After installing a fresh Stretch image, I put all the programs and required libraries back on, a quick test and that all worked.
OK, now it is time to have the programs start automatically at boot.
That is also simple, I usually add the programs I want to start at boot in my /etc/rc.local file.
Always worked for me.
However, this time it didn’t and for the life of me I couldn’t figure it out. Why is a setup that used to work suddenly not working anymore, starting the programs at boot, while the programs itself work when started manually.
Trying trying trying. Writing the output from each command to a log file didn’t help me, logfiles stayed empty.
Then I decided to write the output of the entire rc.local file to a logfile with:
exec > /tmp/rc-local.out 2>&1;set -x
put that in the rc.local file right before the line that says “# Print the IP address”:
I rebooted and checked the file: /tmp/rc-local.out.
Now it became clear…. the paho (mqtt) module that these python programs were using was not found.
Odd.. I am sure I installed it……and when I manually start the programs they work 100%
I was thinking….. in my previous setup, I had installed the paho.mqtt library manually. It resided in a folder in my /home/pi directory. This time I had installed it with pip. Now it wouldn’t be that…… no, it couldn’t, could it?
The commands in the rc.local file, as far as I know are given by root and my /home/pi directory was owned by ‘pi’…….but that hadn’t changed between two installs and root has privilege anywhere right?
After some more searching I learned that the library “paho” had been installed by pip in the folder “/home/pi/.local/lib/python2.7/site-packages” but “sudo python” searches this library in the folder “/usr/local/lib/python2.7/dist-packages”.
I was not using sudo… but that is in fact what the rc.local does, so I was guessing that that was where my problem lied.
So, I needed to link the one directory to the other somehow.
This is what did that:
cd /usr/lib/python2.7/dist-packages
sudo ln -s /home/pi/.local/lib/python2.7/site-packages/paho
Then it all worked again.
Interesting info on how to start your programs at boot can be found here.