0

I need to rotate logs files created by node processes as file sized keeps on increasing. Application runs in ubuntu on aws ec2 instances.

I have few forever node processes running which writes logs into below files.

forever.log
stderr.log
stdout.log

My CloudWatch Agent also looking at these files and pushes the logs to cloudwatch and node applications also continuously write logs into these files.

I need to find the best way to rotate these log files and clean up older files without affecting any of the above processes.

unsure below approach going cause troubles since it's going to move the file and going to create a new file

#!/bin/bash 
touch /script_logs/test.log
MaxFileSize=2048
while true
do
     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
    fi

done

Need to implement a log rotation mechanism without affecting Cloudwatch agent which reads the log file or the application which write logs

4

0 回答 0