After searching for some time- I can;t find the reason that my python3 code won't execute during boot. I'm using Rpi Zero, using StrechLite OS. Code run OK when is executed from command line.
crontab contains:
@reboot /usr/bin/pigpiod
@reboot /usr/bin/python3 /home/guy/Documents/github/Rpi/SmartHome/tst.py &
try 1:
I've created a bash file (containing echo "guy" >>log.log
)to see if cron job is running OK- and redirected its output to a text file. All went well.
try 2:
bash file was edited to run
( python3 /home/guy/Documents/github/Rpi/SmartHome/tst.py
) , but it failed.
try 3:
a simple python code was written, 123.py
containing a single line: print("GUY")
to be executed as : @reboot /usr/bin/python3 /home/guy/Documents/github/Rpi/SmartHome/123.py >>log.txt
in order to check if something is python code during boot time, make it fail- but still python code is not executed during boot.
What is done wrong in order to run a python script this way?
-
What's in your python script? That might be failing due to path issues.Brick– Brick2018年03月12日 22:34:16 +00:00Commented Mar 12, 2018 at 22:34
-
crontab outputs information to /var/log/cron.log - anything there to indicate what the failure is?Jaromanda X– Jaromanda X2018年03月12日 23:00:06 +00:00Commented Mar 12, 2018 at 23:00
-
The pigpio daemon needs to be run as root. Best to put the first line in the root crontab. Also the daemon may take a second or so to launch. Perhaps you need a delay before launching your script if it is using pigpio.joan– joan2018年03月13日 02:18:02 +00:00Commented Mar 13, 2018 at 2:18
-
@Brick path was checked and is correctguyd– guyd2018年03月13日 03:45:30 +00:00Commented Mar 13, 2018 at 3:45
-
@joan pig poof is running ok. The problem is execution of second lineguyd– guyd2018年03月13日 03:47:06 +00:00Commented Mar 13, 2018 at 3:47
1 Answer 1
After trying solving it with creation of daemon service (systemctl
), which yield same unwanted result - I found the reason why:
using in crontab
or daemon service - couldn't get $PYTHONPATH
in time ( or boot sequence ) to find path needed to run a certain module I use.
so I added to my python code: sys.path.append('/home/guy/Documents/github/Rpi/GPIO_Projects/lcd')
-in oder to find that particular module handling LCD display.
BTW- the above path is part of $PYTHONPATH
, which probaby update after logging in, or running python IDE, but not when trying to automatically execute a code after OS boots up ( without logging in ).
Guy