首页 > 解决方案 > Set line maximum of log file

问题描述

Currently I write a simple logger to log messages from my bash script. The logger works fine and I simply write the date plus the message in the log file. Since the log file will increase, I would like to set the limit of the logger to for example 1000 lines. After reaching 1000 lines, it doesn't delete or totally clear the log file. It should truncate the first line and replace it with the new log line. So the file keeps 1000 lines and doesn't increase further. The latest line should always be at the top of the file. Is there any built in method? Or how could I solve this?

标签: linuxbashsed

解决方案


Your chosen example may not be the best. As the comments have already pointed out, logrotate is the best tool to keep log file sizes at bay; furthermore, a line is not the best unit to measure size. Those commenters are both right.

However, I take your question at face value and answer it.

You can achieve what you want by shell builtins, but it is much faster and simpler to use an external tool like sed. (awk is another option, but it lacks the -i switch which simplifies your life in this case.)

So, suppose your file exists already and is named script.log then

maxlines=1000
log_msg='Whatever the log message is'

sed -i -e1i"\\$log_msg" -e$((maxlines))',$d' script.log

does what you want.

  • -i means modify the given file in place.
  • -e1i"\\$log_msg" means insert $log_msg before the first (1) line.
  • -e$((maxlines))',$d' means delete each line from line number $((maxlines)) to the last one ($).

推荐阅读