1

I've run lots of cron jobs and have set up scripts to run at startup. I assumed this would be an easy task because maybe I'm missing something.

I have a script that when it sees a specific message over its "TX/RX" pins it starts recording the data. The python script works as desired. Yet, when I have it run the script at bootup this no longer sees the commands, records the data, and sends the data back (over the same serial). I see the script running on a thread but yet these GPIOs aren't communicating with peripherals. Do you need to do something special to have the GPIO pins available to a script running at bootup?

I'm mostly wondering if what I'm trying to do is possible and if I'm missing something obvious?

BBridges
  • 35
  • 1
  • 6

2 Answers2

2

This happens occasionally when cron is used to start programs under the @reboot schedule.

It happens because cron does not check that all required resources are available before starting the program/script you've specified. For example, if your program requires network resources, cron does not check that the network is available before starting your program. Instead, it just starts your program with the potential result that the program will throw an error if the network resources aren't yet available.

Prepending a brief sleep prior to starting your program is the easiest fix:

@reboot sleep 20; /path/to/your/script 

FWIW, re-directing any stdout & stderr from your script to a logfile can help you capture error messages:

@reboot sleep 20; /path/to/your/script >> /home/pi/mycronlog.txt 2>&1

Alternatively, you can use systemd to start your program. It's a bit more complicated than cron, but has the ability to postpone starting a script until after all required resources are available. systemd is a feature of Linux, and there are multiple tutorials to help you get started. There are also similar Q&A here on RPi SE that may be helpful.

Seamus
  • 23,558
  • 5
  • 42
  • 83
1

This is somewhat of a workaround. It appears that at times the script can run before the pi has had a chance to boot up completely. Putting a sleep(30) just after my Imports fixed the issue. It ensures the logic of the script is executed after critical system functions are in place.

BBridges
  • 35
  • 1
  • 6