IO Redirection – How to Limit Log File Size Using >>
io-redirectionlimitsize;
How can I limit the size of a log file written with >> to 200MB?
$ run_program >> myprogram.log
Best Answer
If your application (ie. run_program) does not support limiting the size of the log file, then you can check the file size periodically in a loop with an external application or script.
You can also use logrotate(8) to rotate your logs, it has size parameter which you can use for your purpose:
With this, the log file is rotated when the specified size is reached.
Size may be specified in bytes (default), kilobytes (sizek), or
megabytes (sizem).
You could write a little bash script to do this. Just tail the file to a certain byte count using tail -c and overwrite the file.
from man tail:
-c, --bytes=N
output the last N bytes; alternatively, use +N to output bytes
starting with the Nth of each file
If the first character of N (the number of bytes or lines) is a `+',
print beginning with the Nth item from the start of each file, other‐
wise, print the last N items in the file. N may have a multiplier suf‐
fix: b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024, GB
1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.
If you are scripting downloads, you should consider using curl instead. Wget can parse output and recursively fetch whole sites, but curl has way more options relating to the actual download of a specific file. Here is the relevant option in the man page:
--max-filesize
Specify the maximum size (in bytes) of a file to download. If the file requested is larger than this value, the transfer will not start and curl will return with exit code 63.
NOTE: The file size is not always known prior to download, and for such files this option has no effect even if the file transfer ends up being larger than this given limit.
The note about this only working for some files is worth considering. The client is dependent on the server to report how big the file is going to be before it starts downloading. Most but certainly not all servers report this.
Best Answer
If your application (ie.
run_program
) does not support limiting the size of the log file, then you can check the file size periodically in a loop with an external application or script.You can also use
logrotate(8)
to rotate your logs, it hassize
parameter which you can use for your purpose: