I want to write a shell script that executes a python script and redirects its output with a timestamp to a different log file every day. The python script should run forever without stopping, it prints small text to the terminal every 10 seconds.
Here is how far I have come:
filename="log-$(date +%Y-%m-%d).log"
python3 -u HelloWord_every10secs.py 2>&1 |
while IFS= read -r line;
do echo "$(date +%x__%H:%M:%S:%3N) $line";
filename="log-$(date +%Y-%m-%d).log"
done >> "log/$filename" #u unbuffered output, for logging, add time stuff
This small code redirects the output of the python code to a log file in log/ directory and ads timestamps. The only issue that is missing it cannot rotate the log file over each day with a different date ending.
I except to have in log/ directory:
log-2022-11-20.log
log-2022-11-21.log
log-2022-11-22.log
... etc.
But I have only one with the date when I run the script.
I know that there is a Ubuntu built-in solution called lograte
for these things, but I would like to solve this problem without that.