I don’t think it will be that simple. Think of what I’m about to write as more or less “pseudo code”, and not literal.
The “tail -n 250
” is just an analogy of a function. If you already have a buffer with the right content, then echo of that buffer to “tail -n 250
”, followed by redirect to a file would work. Then your app could release any line at the head of the buffer exceeding the already saved 250 lines, followed by appending more lines until it is back up to 500 lines…rinse and repeat to echo the last 250 lines via something like “tail -n 250
” of the buffer, and again delete the first 250 lines in the buffer.
Thus the problem with something “simple” is that it needs to accumulate a buffer, recognize when 250 lines are available, and then trigger logging of those 250 lines to a new file name.
When it comes to files in “/var/log
” there is a log rotation program. This does not directly switch to a new log name. What it does is recognize the need to move the old file name to a new file name, and then start logging again in the original old file name (which is now empty). This does not switch files in the middle of a log unless it first stops logging and then restarts logging.
There are standardized tools which are used for this more “simplified” log rotation which you could use to trigger rotation just by using what’s already part of Linux logging. Or you could make your script recognize when it has logged 250 lines, and then switch to a new log file. If this also looks to see how many log files you have, then it could remove log files which exceed some chosen count, e.g., it could keep two log files, and if it sees a third created, then delete the one oldest file to leave two log files.
This isn’t really a direct answer, but there are several articles on setting up “standard” log rotations in Ubuntu (mostly this is the same on every distribution, but the Jetson runs Ubuntu, and Mint is very similar):
Search for ubuntu+how+to+rotate+logs
Personally, just because I like things to work the exact way I want, I’d be tempted to spend time to create a bash
script (you already have a bash
script, just simple) which logs with custom scripting. This isn’t for everyone, especially if you are in a crunch for time, and I am a bit odd that a custom script is somewhat entertaining to build (if I have time).
If you were to use a custom script, then you’d need to specify if the script needs to handle the system shutting down in the middle of running, e.g., from reboot. If you don’t care if reboot loses the last few lines of log, then it is quite simple.
On the more complicated side you could even create a service (systemd
) to automatically start your program and handle what to do for truncating versus saving of the last few log lines upon shutdown/reboot. To really answer this you’d need to provide details of what you really require, versus what you would desire, along with whether you have the time to work on something custom. If not enough time or desire, then probably learning how to use the log rotation method which the above URL search is for would be your best bet.