Run a py script at boot up on a Pi Zero

Hi all.

I’m happy with the fundamentals of the Pico and have got a few HA projects running. Now I want to step up to do the same with a headless Pi Zero 2 W.

I’ve got a simple script (see below) that blinks a led when the script is run. I can run it from Thonny via SSH no problems.

To get this to execute on boot I have made the file executable with:

chmod +x /home/morchard/Testblink.py

I’ve then edited crontab using nano to add the line:

@reboot /usr/bin/python3 /home/morchard/Testblink.py >> /home/morchard/logfile.log 2>&1

When I reboot the zero the LED does nothing. I can see “Blink script started” in logfile.log so I know the script ran and there are no errors in the log file.

Copilot wants to take me down a investigative rabbit hole (as usual) and suggests a GPIO conflict that I can’t pin down.

I’m starting with crontab as its supposed to be the simpler option. Once I can get that to work I’ll do the same in systemd.

Insights welcome!

from gpiozero import LED
from time import sleep
led = LED(25)

with open("/home/morchard/logfile.log", "a") as f:
    f.write("Blink script started\n")
 
sleep(30)
 
while True:
    led.on()
    print("On")
    sleep(1)
    led.off()
    print("Off")
    sleep(1)

Not sure how it will handle the same file being open as the “redirect” AND in the script itself. I assume the one in the script will fail as its already open from the command line in a redirect (maybe ?).

Can you try not opening it in the script and just let the redirect log the prints.

1 Like

Hi @Mark285907 ,

I have a few suggestions. Firstly, you will need to make sure crontab runs your code as a root user or at least a user that has GPIO permissions. I would try adding your line to the root crontab using sudo crontab -e.

If this doesn’t work, it’s also possible that your code is running on startup before the system has time to bring the GPIO online. Adding a small delay before your code runs normally gets around this @reboot sleep 10 && /usr/bin/python3 /home/morchard/Testblink.py >> /home/morchard/logfile.log 2>&1

Let us know how you go!

1 Like

Thanks Michael,

I’ll remove the “with open” from the script.

Thanks Sam.

I should have said that I used Sudo crontab -e, open in Nano and added my @reboot line.

I will try adding the delay and report back.

For the OS I installed 32 bit Trixie with the desktop. Is this ok or should I have gone with Trixie and no desktop?

Hey @Mark285907 ,

That shouldn’t really make a difference in this case. Your boot time may be a little longer as you’re loading a desktop environment as well, but this shouldn’t affect your script or crontab usage.

1 Like

AAAHHHGGGG…………….

‘Twas a bloody intermittent cable fault!

For house keeping, I have the Zero mounted in a SunFounder RAB Holder Breadboard Kit for Raspberry Pi 5/4, Arduino Uno R4 Minima/Wifi/Uno R3/Mega R3 | SunFounder

Turns out the ribbon cable is intermittently faulty on the end plugged to the Zero.

The LED would blink when I manually run it, jumpered direct to the Zero.

I’d fit the ribbon cable, manually run and all’s well. Then I’d muck around setting up the auto start files and it wouldn’t work. No doubt I’ve moved the ribbon.

Anyway, jumpered direct to the Zero and it worked first time as coded above, ex an delays or log file writes.

Sigh………………..

1 Like

It might be the GPIO Hammer Headers - Solderless Raspberry Pi Connectors - Male + Female + Installation Jig | Buy in Australia | PIM296 | Core Electronics I’ve used?

1 Like

Hi there, @Mark285907,

Glad to hear that you got it working. Cable faults are so simple and yet one of the most common causes of problems with one’s setup.

I’m not entirely certain what you mean by this comment, however.

Thanks Jane.

I thought that perhaps the intermittent fault was the solderless header not making a solid connection to the Zero’s PCB but I’ve tested again and I’m sure its the ribbon cable where it clamps into the 40 pin connector.

1 Like

The solderless hammer headers grip the boards tightly so it makes a lot more sense for the ribbon cable to be at fault.

1 Like