There is a python script, set the task in cron to execute it once a day.

14 02 * * * cd /home/user/project/tools/ && ./script.py > /home/user/project/logs/log_script 

I check the log file and find out that every day the log is new, that is, yesterday's data is overwritten by new ones.

Tell me how to set up to write to the file, and not grind it every day?
Well, absolutely fantastic if it were added to the beginning of the file, and not to the end.

  • one
    replace > with >> - KoVadim

2 answers 2

but if you really want it at the beginning, then you can somewhere

 cd /home/user/project/tools/ && ./script.py > ../logs/log_script_t && cat ../logs/log_script >> ../logs/log_script_t && rm ../logs/log_script && mv ../logs/log_script_t ../logs/log_script 

line by line

output to a temporary file

 cd /home/user/project/tools/ && ./script.py > ../logs/log_script_t 

and now let's add the contents of the log to it.

 && cat ../logs/log_script >> ../logs/log_script_t 

delete the file with the log

 && rm ../logs/log_script 

and rename the temporary file

 && mv ../logs/log_script_t ../logs/log_script 
  • Or so cd / home / user / project / tools / && ./script.py> ../logs/log_script_$$ && touch ../logs/log_script && cat ../logs/log_script >> ../logs/ log_script _ $$ && mv -f ../logs/log_script_$$ ../logs/log_script touch needed if ../logs/log_script has not yet been created, and $$ in ../logs/log_script_$$ will allow several scripts work to some extent at the same time. - avp
 > заменить на >> 

To put the log in the beginning, you need a certain number of variables, magic, cat and echo, I can’t help here, but this construction is unlikely to fit the line adequately.