diff --git a/userspace/rootfs_override/wr/bin/log_rotate.sh b/userspace/rootfs_override/wr/bin/log_rotate.sh index 2b8120aacf2dca5d20641db91ce55fdf96a4f0aa..7fdb91197c99ddb063f81b4935143fe89663b98d 100755 --- a/userspace/rootfs_override/wr/bin/log_rotate.sh +++ b/userspace/rootfs_override/wr/bin/log_rotate.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash ############################################################# # Maciej Lipinski @CERN @@ -27,47 +27,38 @@ fi # Set number of rotated logs, fron input or default if [ -z $2 ]; then log_numb=10 +elif [ $2 -lt 2 ]; then + log_numb=2 else log_numb=$2 fi echo "Rotate log file $log_file $log_numb times" -# Just in case, check whether the log file exists +# Just in case, check whether the log file exists if [ ! -f "$log_file" ]; then echo "No logfile $log_file" exit 1; fi -# Move the log file from rsyslogd into the rotate file -# The date is an integer so that it is easy to recongize -# the oldest file. +# Rotate old syslog files, indexed by numbers (higher values +# for older files). Numbers are used, not the date, because +# the date can change, e.g. as a result of PTP synchronization. +# In this process, we discard files with max-specified number +# (ie.e. $log_num). +i=$log_numb; +while [ $i -gt 1 ]; do + printf -v new_rotate_file "%s-%02d" $log_file $i; + i=$(($i - 1)) + printf -v old_rotate_file "%s-%02d" $log_file $i; + echo "move $old_rotate_file to $new_rotate_file" + # Discard any errors when using mv, errors happen at the begining + # when there is no rotated files. + mv -f $old_rotate_file $new_rotate_file 2> /dev/null +done -new_rotate_file=${log_file}-$(date +%Y%m%d%H%M%S) +# move the syslog file as to the rotated files with the lowest +# index because it is the newest +new_rotate_file=${log_file}-01 mv -f $log_file $new_rotate_file echo "Move $log_file to $new_rotate_file" - -# Remove excess rotate file. Just in case, there are -# more than a single excess rotate file, remove excess -# files until happy with its number. - -# Print the expected and current number of rotate files (just for info) -log_cnt=$(/bin/ls ${log_file}-* | /usr/bin/wc -l) -echo "Number of rotate files is $log_cnt and should be max $log_numb" - -# Remove any excess files -while [ 1 ]; do - - # Check the number of rotate files. - log_cnt=$(/bin/ls ${log_file}-* | /usr/bin/wc -l) - - if [ $log_cnt -gt $log_numb ]; then - # Remove the oldest file if there are too many rotate files. - oldest_rotate_file=$(/bin/ls ${log_file}-* | /usr/bin/head -1) - echo "Remove oldest file: $oldest_rotate_file" - rm $oldest_rotate_file - else - break - fi -done -