Open File keeps growing despite emptied content

不羁的心 提交于 2019-12-12 04:25:20

问题


How can I pipe a text stream into a file and, while the file is still in use, wipe it for job rotation?


Long version:

I've been struggling for a while onto an apparently minor issue, that's making my experiments impossible to continue.

I have a software collecting data continuously from external hardware (radiotelescope project) and storing in a csv format. Being the installation at a remote location I would, once a day, copy the saved data in a secure place and wipe the file content while, for the same reason, I can NOT to stop the hardware/software, thus software such as log rotation wouldn't be an option.

For as much effort spent see my previous post, it seems the wiped file keeps growing although empty.

Bizarre behavior, showing file size, truncate file, show file size again:

pi@tower /media/data $ ls -la radio.csv ;ls -s radio.csv;truncate radio.csv -s 1; ls -la radio.csv ;ls -s radio.csv
-rw-r--r-- 1 pi pi 994277 Jan 18 21:32 radio.csv
252 radio.csv
-rw-r--r-- 1 pi pi 1 Jan 18 21:32 radio.csv
0 radio.csv

Then, as soon as more data comes in:

pi@tower /media/data $ ls -la radio.csv ;ls -s radio.csv
-rw-r--r-- 1 pi pi 1011130 Jan 18 21:32 radio.csv
24 radio.csv

I thought to pipe the output into a sed command and save right away, with no luck altogether. Also, filesystem/hardware doesn't seems buggy (tried different hardware/distro/filesystem).

Would anyone be so nice to give me a hint how to proceed?

Thank you in advance.


回答1:


Piped into tee with -a option. The file was kept open by originating source.

Option APPEND of tee helped to stick at the EOF new data; in the given issue, appending to the beginning when file zeroed.

For search engine and future reference:

sudo rtl_power -f 88M:108M:10k -g 1 - | tee -a radio.csv -

Now emptying the file with

echo -n > radio.csv

gets the file zeroed as expected.



来源:https://stackoverflow.com/questions/28013256/truncate-open-file-doesnt-affect-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!