V
V
Vladimir2021-02-24 14:33:43
linux
Vladimir, 2021-02-24 14:33:43

How to run a task in the background on Debian and only write the last line of the console to a file?

I need to run a python3 script in the background and write the contents of the console to a file to be able to control the execution, but in this case it quickly fills the file, since it transfers a lot of data to the network and to the console, how to make it always log did the file write only the last line, and did not accumulate the full contents from the moment it was launched?

/usr/bin/python3 test.py >> test.log &

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Fenrir89, 2021-02-24
@Fenrir89

/usr/bin/python3 test.py | tail -n1 >> test.log &
Another option is to cut off the excess using grep

I need that at the time of opening, I saw in the test.log file, for example, only 5, or 255, depending on how long it worked

Then like /usr/bin/python3 test.py > test.log &
>> append to file
> overwrite file

S
Saboteur, 2021-02-24
@saboteur_kiev

how to make it so that it always writes only the last line to the log file, and does not save the full contents from the moment it was launched?

there are about 50-60 iterations per second, so many times in the script itself, saving is not very relevant, probably

Can you humanly formulate which line of which second of which iteration you want to see in the log?
PS I suspect that as soon as you formulate it normally, you will immediately understand how to do it yourself.
PPS
Output in python itself to a file, without append, so that it writes only one line, and output to /tmp, which in most Linuxes is ramdrive Better
yet, in python itself, output not every value, but one of 10 or out of 100. Well, it's not difficult.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question