how to implement logrotate in shell script

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.

touch /script_logs/test.log
while true
     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.

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.