Log rotate
At first it was unclear to me why you chose this approach rather than take advantage of the various log rotate options that are available in Traefik (docs).
But it turns out that Traefik does not help with access logs; and you have to manage them yourself.
Usually, I prefer to rely on Linux logrotate for this kind of job. The fact that we are running in Docker does not preclude us from using that kind of mechanism.
Truncating logs
First of all, does the script run as expected? I get this error:
return: can only 'return' from a function or sourced script"
Which makes sense. return should be used to stop the execution of a Bash function. Is the script meant to be called directly?
Probably you mean exit 0, but that is the default exit code anyway. Only when you want to return a non-zero value, should you use exit.
The log file format is in plain text format and not in JSON format, so
the log file should still be valid after cleanup
Let's hope so. But I would not be so certain. Some applications may log multi-line text separated by \n, optionally enclosed within quotes. For example the exception stack trace.
Counting lines
Instead of:
lines=$(wc -l "$fn" | awk '{ print $1 }')
You can do it without awk or cut - look at the subtle difference:
lines=$(wc -l < "$fn")
But I find the approach wasteful. Counting lines requires some disk reads and CPU cycles, and this could take seconds on a large file.
Instead, we should just rotate the log file once it has reached a certain size in bytes.
I am not fond of truncating the file in-place. Just rotate the whole of it, and start with a new file. Zip the last few log files for reference.
Keeping a backup
A small tip anyway, useful for testing at least:
sed -i.bak "1,${lines_to_del}d" "$fn"
This will keep a copy of the original file, appending a .bak extension so you can compare changes, and recover from errors.
Error handling
At this time, there is no real error handling, other than the set -e command which tells the shell to exit immediately if a command exits with a non-zero status.
I recommend using the trap function in Bash to handle errors or run cleanup routines. This script does one thing that is dangerous: altering a file. On top of that, the file is being used by a critical piece of software.
So it's useful to retain a backup, so that we can recover from any error occurring during this sensitive operation.
Your script should also check that it is touching a file, and not a directory for example. What would happen then?