I would like to know how to copy all content of a file (server.log) to a new file and removing the content from original file by using Linux commands. Actually it is easy to to that. But I actually want to be sure there will not be content update within that operations. Following Linux commands do what i want, but I have to be sure there is no change in server.log between command1-command2 execution.
command1: #cp server.log serverNew.log
command2: #truncate -l 0 server.log
I would use a tool especially built for this purpose instead of using some ad hoc solution.
Take a look at logrotate. You can use the command directly or set it up in a cron job.
It’s supports compression, running command after each rotation, rotating based on size or time etc…
Based on your comment below I assume you are interested in these options:
postrotate/endscriptThe lines between
postrotateandendscript(both of which must appear on lines by themselves) are executed (using/bin/sh) after the log file is rotated. These directives may only appear inside a log file definition. Normally, the absolute path to the log file is passed as first argument to the script. Ifsharedscriptsis specified, whole pattern is passed to the script. See alsoprerotate. Seesharedscriptsandnosharedscriptsfor error handling.prerotate/endscriptThe lines between
prerotateandendscript(both of which must appear on lines by themselves) are executed (using/bin/sh) before the log file is rotated and only if the log will actually be rotated. These directives may only appear inside a log file definition. Normally, the absolute path to the log file is passed as first argument to the script. Ifsharedscriptsis specified, whole pattern is passed to the script. See alsopostrotate. Seesharedscriptsandnosharedscriptsfor error handling.