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?

3
  • Any access issues should error and be caught by your error handling code (assuming you have some). Does you program log anything? May be worth adding a few debug log / print commands and capture stdout and stderr to files. I take it you have checked the user the background job runs as has access to all objects the program needs? Do you have a console input or anything left over?
    – user130616
    Commented Apr 24, 2021 at 21:03
  • 1
    I finally found a solution. Simply adding a 30sec delay at the start of the code fixed the issue. I don't completely understand why?!? Either way, it's working now.
    – BBridges
    Commented Apr 24, 2021 at 21:25
  • I would put that as an answer but note it's a workaround and accept it. That will stop the question coming back here for years :-)
    – user130616
    Commented Apr 24, 2021 at 21:39

2 Answers 2

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.