0

I have a log file I track on my own. I am running a python script on raspberry pi and printing messages at various parts/events of the code. I have the script outputs piped to a text file. Some cycles all messages are logged and other cycles hardly anything is logged. The exact same events happen each cycle so I should see the same messages logged. What would cause this to happen? Is this a python issue or linux issue? The script does not crash but the logging is inconsistent.

example command running in my /etc/rc.local folder:

sudo python test.py >> log.txt&
SChand
  • 51
  • 1
  • 9
  • 1
    Would be useful if you can clarify how you are setting up the `loghandler` with some sample code. Flushing your log messages `sys.stdout.flush()` may help if you are writing `std out`. Also note that you are not redirecting the error steam. `sudo python test.py >> log.txt 2>&1` – Max Arbiter Jun 23 '20 at 19:49
  • @MaxArbiter I am not using loghandler. It's simply just status updates I was pushing to my text file. – SChand Jun 23 '20 at 20:10

1 Answers1

1

Try

sudo python test.py >>log.txt 2>&1

Here:

  • >>log.txt: opens the file in append mode and redirect stdout there.
  • 2>&1: redirects stderr to where stdout is currently going. In this case, it is your log file opened in append mode. &1 reuses the file descriptor which standout is currently using.
Ugnes
  • 709
  • 1
  • 9
  • 23