0

i've got some problem with rotating files. Situation is next:

  1. one program like wireshark or vlc dumps all to one file, say netstream.bin
  2. If i mv netstream.bin to netstream.bin.rotate program will still write to netstream.bin.rotate .
  3. As it is stream dump, this file would became larger and larger

Is there any solution, how to rotate this files?

Something like FILO pseudo file:

mkfilo /tmp/stream.buffer
./scrtip/program.bin -o /tmp/stream.buffer
get_out_filo(stream.buffer) > netstream.bin

My question also sounds like "How to change programs file descriptor to write another file"

HopelessN00b
  • 53,795
  • 33
  • 135
  • 209
MealstroM
  • 1,517
  • 1
  • 17
  • 32

1 Answers1

1

A fifo may not work so nicely for you. There must be a reader on the other side, else the writer will block. Additionally once the reader is no longer reading the fifo the writer will exit with a broken pipe.

logrotate can help you mostly, but it comes at the cost of possibly losing some data.

  1. Append into a given file in append mode (which most logging systems will do).
  2. Whilst the data is being written, copy the entire file verbatim to a new name.
  3. Truncate the original file to zero, keeping it where it is.

The problem of losing data occurs between step 2 and 3. It is possible that more data gets written to the file after the file copy has copied what it thought was the end of the file. When step 3 occurs, you truncate (and lose) that data you missed during the copy.

Depending how much you need this, an alternative would be to write a program that is always reading from a fifo file, yet writes the data out to different places dependent on some condition (such as it receives a signal). This would be a clean way to do it but you'd need to write that program first!

Matthew Ife
  • 23,357
  • 3
  • 55
  • 72