0

When I start the script test.py > test.log, the output is only written to the file test.log after the script has finished. How do I get the output to the test.log file while the script is running (live)?

I have tested without success: ./test.py > test.log ./test.py &> test.log ./test.py > test.log &

nohup python3 -u test.py > test.log 2>&1 & works. But when multiple scripts are running, all tasks are named python3. This is unfavorable eg for the killall -9 command.

test.py

#!/usr/bin/python3
from time import sleep
for i in range(0,21):
  print(i)
  sleep(1)
Daniel L.
  • 1
  • 1

1 Answers1

0

This is very similar to the discussion in this question.

In your described situation, setting the PYTHONUNBUFFERED environment variable should do what you need.

goatshriek
  • 133
  • 6
  • Upon further experimentation this doesn't seem to work with your example test script, though the `-u` parameter does. Adding manual calls to sys.stdout.flush() and the flush=True statement to the print call also do not appear to help, I'm not sure why. – goatshriek Feb 25 '23 at 18:37
  • Ok thx. When I run the script with **python3 -u test.py > test.log** it works. But the task is named **python3**. – Daniel L. Feb 26 '23 at 19:47