1

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?)
3
  • It is likely a buffering issue. The Python code is not producing enough output to warrant flushing the output buffer, and it never flushes it explicitly. See either unix.stackexchange.com/questions/324824/… or unix.stackexchange.com/questions/182537/… – Kusalananda Jan 2 at 19:04
  • Actually it was 2 issues. First the one you mentioned, second for some reason it did not output anything when it was ran from 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 2 at 21:32
  • That could be because rc.local wasn't running with bash but with /bin/sh? In any case, it should be running as root, so you should not need sudo in there. – Kusalananda Jan 2 at 21:33

Browse other questions tagged or ask your own question.