I am working on creating a python script to retrieve data from a server using the requests library, and then display it on an adafruit led matrix using a raspberry pi. I have a program that functions as expected when called from the terminal. However, I want the program to start running immediately whenever the pi boots up. So I added it to the crontab with @reboot in front of it. However, when upon reboot, the program runs for only a split second, then stops. I have narrowed the issue down and I know which line causes it. Here is the method with the error-causing line:

        request = requests.get('http://jacob-t-graham.com/led-data')    #make a request to the led data page of the site
        print(f'Request returned status code {request.status_code}.')
        raw_string=request.text #get the raw text from the request
        color_ls=raw_string.split(',')    #split the string around commas
        ls_of_lss=[color_ls[x:x+self.cols] for x in range(0, len(color_ls), self.cols)]    #split the list into lists of the appropriate size
        return ls_of_lss

The line that calls the requests.get method is what causes the problem. If I comment that line and reboot, the program runs as expected. However, the strange part is, when I try to capture error output from the program when that line is in, nothing about the error shows up in the log file. Again, this code runs fine with the error-causing line when run using a terminal. So, how can I get this program to run on boot via the crontab?

  • 2
    Try @reboot (sleep 30; your_command) to see if a short delay helps.
    – joan
    Jul 23, 2022 at 8:44
  • As joan mentions add a delay, your Pi may not have connected to your network when you attempt your get.
    – CoderMike
    Jul 23, 2022 at 9:59
  • There is a possibility that the permissions of the script is wrong, or the environment specifically PATH and such are set up incorrectly.
    – NomadMaker
    Jul 23, 2022 at 21:25
  • 1
    It might be better to run this as a systemd service instead of a cron job. Then you can tell it to delay starting until relevant services are up.
    – user10489
    Jul 24, 2022 at 0:54
  • adding the delay fixed my problem! Thanks for the help, it must not have been connected to my network when it tried to make the get request. I may also look into systemd, but the delay with cron worked well, thank you!
    – ho88it
    Jul 24, 2022 at 23:58


Your Answer

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

Browse other questions tagged or ask your own question.