I'm trying to run a python program at startup by putting the following lines into etc/rc.local
:
(cd /home/pi/CODE/weather-project/weather-station &&
python main.py) &>> log.txt &
The program runs fine, but log.txt is empty, my print statements are not logged into log.txt, except when I kill the Python process it'll contain Terminated
. It also works fine when I remove the &
sign from the end (but this is something I do not want).
How do I get bash to write the output of a Python program into a file if this program runs in a subshell. I'm using Bash 5.
This is the contents of my log.txt
when running the script from the terminal for a few seconds without the &
character at the end of the command:
Initializing EPD...
VCOM set to -2.06
current time is: 2021-01-02 19:32:48.198797
refresh weather
heat data is not stable (yet?)
rc.local
. The solution was to invoke it this way:cd /home/pi/CODE/weather-project/weather-station && sudo bash -c 'python -u main.py &>> /home/pi/CODE/weather-project/weather-station/log.txt' &
– sydd Jan 02 '21 at 21:32rc.local
wasn't running withbash
but with/bin/sh
? In any case, it should be running as root, so you should not needsudo
in there. – Kusalananda Jan 02 '21 at 21:33