echo "Hello World"


while true
    sh test.sh >> /script_logs/test.log &

I want to implement logrotate to control the log file size, so how to implement the logrotate, if the situation is like above?

  • NOTE for the unwary: Most operating systems will let you delete and rename a file (such as a log file) even while another program still has it open and is still writing to it. So it may still be possible that an opened log file will continue to grow in size even when you try to rename/rotate them. And trying to truncate the log will likely cause the program writing to the log to do strange things because it is not expecting the log file to suddenly become empty. Caveat emptor.
    – C. M.
    Apr 22, 2021 at 12:43

6 Answers 6


how about using savelog?

It's available in debian and RH and pretty much every other linux distro I know of. It's a /bin/sh shell script, so should run on any other unix too.

e.g. before writing anything to test.log run savelog -n -c 7 test.log. This will keep the 7 most recent non-empty versions of test.log. By default, it will compress rotated logs (but that can be disabled with -l).

If you need to, you can check the size of test.log and only savelog it if it is over a certain size.

  • Any idea which package contains this on AWS Linux (close to CentOS, I believe)? Default installation does not have savelog. I can't find it from yum either using default repos
    – jhonkola
    Feb 25, 2016 at 11:50
  • savelog not found in RHEL 5. Do you know which repo it should be in? Apr 18, 2016 at 0:23
  • 2
    I haven't found savelog on any of my RHEL/CentOS 5/6 boxes, so I've just downloaded it ad hoc, and it seems to work just fine for my needs. May 15, 2018 at 17:48
  • 1
    One downside of savelog is that it renames the file but then takes a long time to gzip the old ones before finishing. Meanwhile the .0 log already gets next days entries in it. Ideally the time between log rotation and signalling the process to reopen the log should be minimal. I disable savelog's compression feature because of this.
    – rustyx
    Oct 13, 2018 at 20:31
  • 2
    On debian distros, package debianutils has it.
    – akhan
    Jul 19, 2019 at 18:28
touch /script_logs/test.log
while true
     sh test.sh >> /script_logs/test.log
#Get size in bytes** 
    file_size=`du -b /script_logs/test.log | tr -s '\t' ' ' | cut -d' ' -f1`
    if [ $file_size -gt $MaxFileSize ];then   
        timestamp=`date +%s`
        mv /script_logs/test.log /script_logs/test.log.$timestamp
        touch /script_logs/test.log


I have removed the "&" as it may cause an issue.

  • @Veerendra we need to put the logic of rolling the logs inside the loop as you are running an infinite loop ..
    – curious
    Sep 23, 2015 at 7:14
  • @Veerendra we my previous post had an error .. "mv" won't create a new file but you can change the script according to your requirement.
    – curious
    Sep 23, 2015 at 7:18
  • So, instead of cp and mv, can I remove the log file, then will it new log file with same name?(My requirement is, if the log file is reached certain limit, I want to remove that log file and then create a new file ) Sep 23, 2015 at 7:23
  • You should use >> to append to log file, > will overwrite it over and over again.
    – alcik
    Sep 23, 2015 at 7:34
  • if I use >, I'm getting tail: test.log: file truncated Hello World!....If I use >>, I'm getting correct log message, but the file size is increasing. No condition checking the condition... ;-( Sep 23, 2015 at 7:52

I wrote a logrotee this weekend. I probably wouldn't if I've read @JdeBP's great answer about multilog.

I focused on it being lightweight and being able to bzip2 its output chunks like:

verbosecommand | logrotee \
  --compress "bzip2 {}" --compress-suffix .bz2 \

There's a lot of to be done and tested yet, though.


below script can be used


# Log directory

# Maximum number of archive logs to keep

#Log files to be handled in that log directory 
files=(access.log error.log)

for LOGFILE in "${files[@]}"

## Check if the last log archive exists and delete it.
if [ -f $LOGDIR/$LOGFILE.$MAXNUM.gz ]; then

NUM=$(($MAXNUM - 1))

## Check the previous log file.
while [ $NUM -ge 0 ]
NUM1=$(($NUM + 1))
if [ -f $LOGDIR/$LOGFILE.$NUM.gz ]; then

NUM=$(($NUM - 1))

# Compress and clear the log file
if [ -f $LOGDIR/$LOGFILE ]; then
cat /dev/null > $LOGDIR/$LOGFILE

  • I added a note to the original question.. Which this answer needs to be aware of. The last steps of compressing and truncating the log file may have very nasty side effects if they execute while some other program still has the log file opened and writing, OR even if the operating system itself still has output to the file in it's buffers. (The buffered issue can usually be solved by giving the sync command to tell the OS to flush all it's buffers.)
    – C. M.
    Apr 22, 2021 at 12:51

As I cannot yet add comments to the accepted answer, a BusyBox hint, where du does not have a -b flag:

du /var/log/file | tr -s '\t' ' ' | cut -d' ' -f1

savelog is good, but I would prefer to have a tool that rotate the log according to date, and it doesn't change the extension of the file:

rotatelog -c 30 program.log

1, if the program.log is not created today, then rename it to program.20221219.log (assume today is 20221219. Assume there is no such file exists yet). 1.1, if the program.log is created today, leave it as is.

2, remove all program.xxx.log that are older than 30 days old.

If I add a cronjob to execute the current savelog once every day, it would be very close to what I am thinking to have, but the filename keep changing, and I don't like it.

  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 21, 2022 at 21:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .