Issue
I have a simple python
file as
test.py:
import time
i = 0
while True:
print(i)
time.sleep(5)
i = i + 1
To run/ test this file I created a bash
file
test.sh:
python '/home/pbm/python/test.py' >> /home/pbm/python/test.$(date +'%d_%m_%Y').log 2>&1 &
Why the value of print is not getting logged in log file? I need the print to be logged in log file and if any error occurs should be logged in the same file.
Solution
the print
is buffering, you need to add flush=True
:
while True:
print(i, flush=True)
...
NB. this is for python 3
Answered By - mozway Answer Checked By - Terry (WPSolving Volunteer)