0

The Python script is unable to work in rc.local, as soon as it nevet gets executed. My idea is to run the script when the Raspberry Pi gets boot.

I have tested it with this sentence. The log.txt file only appears when I execute the program manually.

f = open("log.txt", "w")
f.write("log is working")
f.close()

Before that, I have tried to insert a time.sleep(30), to use usr/bin/python3, to change the head of the script to #!/usr/bin/env python3, change the user is executing the program to -u pi and a lot of things I can't even remember.

The final sentence is had before exit(0) is

sudo /usr/bin/python3 /home/pi/script.py &

rc.local is working as soon as it runs an echo that I have create in the file.

Guille
  • 11
  • 2
  • Try a leading `/` in the path to python3, like `/usr/bin/python3`. – jamieguinan Jun 22 '21 at 13:16
  • To start with put `#!/usr/bin/python3` and not `#!/usr/bin/env python3` . Then if you use the line `#!/usr/bin/python3` then you just have to make the script executable with `chmod` and a simple call to `sudo ./yourscript.py &` is necessary You can use #!/usr/bin/env python3 for portability across different systems in case they have the language interpreter installed in different locations. if rc.local break really problematic try to go through init.d – Jerome Favrou Jun 22 '21 at 14:31
  • Have you set the execution bit? `chmod +x /home/pi/script.py` Running it as a python module with `python3 -m script.py` wouldn't require that, but running it as it's own script does. Also, being a log, you might want to use append mode `f = open("log.txt", "a")` instead of write-over mode. – Doyousketch2 Jun 23 '21 at 07:56

1 Answers1

0

Finally the problem I had was that the script needed network, so I added it to crontab -e.

It still didn't work so I changed the raspi-config, as there is an option for network wait to network, but without success.

Finally, as that solution didn't work too, I added an sleep in the command as follows to wait for network:

@reboot sleep 40 && /usr/bin/python3 /home/pi/script.py 

That finally worked.

Guille
  • 11
  • 2