Python3 Script on Raspberri Pi @ Startup

I can open a terminal to run my python script and it works perfectly. (enabling a piicodev potentiometer to control system volume) Though, attempting to autoload it on startup is being a pain.

I’ve tried the methods crontab and systemd.
Crontab will work, but my output file give me errors about connecting to alsa audio

Systemd works as well. When checking the status via:

sudo systemctl status myscript.service

It shows that it’s actively running, but the potentiometer is not functioning.

Again, I can run it via command line manually and it works well.

What might be the issue? I’m becoming exhausted for the 4th time trying to figure out what might be getting by me here.


1 Like

alsaaudio will need to wait for the audio engine service to kick in. It may be that you’re loading the python script before alsaaudio is done booting. :man_shrugging:

Is it possible to add a 60 second delay in your Systemd file?

Alternatively I found the method I used for my Parisians project to be 100% reliable so far :slight_smile:
Feel free to copy that.
Here is a link to the github where I go into detail.

1 Like

Suggest placing your run command in /etc/rc.local
It is one of the last things run when the Pi is booting up, so all the other stuff should be available as per Pixmusix suggestion.


EDIT: place an & after your command in rc.local to tell the system to fork the process. This allows the rest of the script to run. Otherwise the system will await the end of your command process before it continues and if your process never ends …
Use the full path to the command in the script.

1 Like

Oh! Thank you! I forgot that I used this method as well. It never seemed to work.

I currently have this in my /etc/rc.local file

python3 /home/joshua/piicodev/ &
exit 0

I have permissions set for the file via…

sudo chmod +x

No dice. :expressionless:


I only added the least amount to the .bashrc file and it worked first try. Thank you!

sleep 5
python3 /home/joshua/piicodev/

Now that it’s working, when I open a terminal it’s only a white square and I can’t enter any commands. I assume it’s being “used” by the running of the python script? Hummm


Yeah that’s right.
You have a different situation to me where you want the script to be running quietly in the background.

You could ofcourse just open a second terminal and minimise the python script.

Im intrigued by @James46717 suggestion.

I’m going to try that. We might both be about to learn something.

1 Like

This is the /etc/rc.local script I used to run the python code for my weather station on a Pi 3B+.
Note rc.local runs before a GUI interface is loaded so anything to do with the screen will fail.

The main Python code does not access the screen it just reads serial input from the sensors and saves it in a file. The Webserver code runs flask and produces a web page graphical output to any device that connects, using the file data.


PS This method has been used successfully on a number of projects.

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

cd /home/pi/WeatherStation
sudo ./ &
sudo ./ &

exit 0

The sleep 5 allows the system to finish starting up before running the Python script.
The terminal situation might be because the python code is not forked as a separate process.

Try an & on the end of the line, like this:

sleep 5
python3 /home/joshua/piicodev/ &
exit 0

Also exit 0 should be at the end of the rc.local script.


Just getting back to this…I can open several terminals and it’s only a solid white vertical rectangle in every window. Like, I can’t get to a console anymore to do anything. :frowning:

Ok for some reason it was not working, but now it is. I can CTRL+C and continue to use my console. AND, the script continues to work so…YEAH!


we take the W when we get Ws :+1:

1 Like

Hi Joshua

Oh Oh! One of those.
Seems to be common in the RPi world. Magic stuff, fixes itself.

YEAH. Until next time. There is a reason for everything including the problem that has remarkably “fixed” itself. All OK until it happens again. BUT, you might get lucky.

That says to me there is a common (common to everything) problem somewhere and the fact it is “fixed” indicates an intermittent one. One of the hardest to pin down.
Cheers Bob

1 Like