Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Log Rolling Script

Status
Not open for further replies.

stu78

Programmer
May 29, 2002
121
GB
Hi,

I need to roll a debug log file every 1 GB of data. I cannot stop/start the processes to do this.

How may this be achieved?

 
Code:
awk -v max=$((1024)) -v Log=a.log 'BEGIN { outlog = Log }
{ if ((byte += length) > max) {
  byte -= max; close(outlog); outlog = Log "." ++stuffix
} print >> outlog }'

take also a look at split(1)

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
sorry, -v max=$((1024**3)) # around 1073741824
 
Whether that or any other method will work depends on whether the logging application maintains an open file handle to the log file. If it opens the file then closes it for every write, then you'll be fine.

If it keeps the file open, then even though you delete the file and re-create it empty, it will still be writing to the original file. If you zero the file out rather than deleting it, it *may* work.
 
Both good posts;

xmb - I will test your code.

ericbrunson - how can I test your method? or is it even possible?
 
Should be around this shorty:

check log && cat log >log.$((++run)) && >log
 
Hi,

Is the check function correct in this post?
If so - can you elaborate how I can use it?

 
Is the check function correct in this post?
Not really.. its symbolic for any check done (to succeed) before cat-copying the log file to log.<number+1> and empty'ing log.
Eg,
Code:
max=$((1024**3)) # 1GB
while sleep 5; do
  (( $(wc -c $log | awk '{print $1}') >= max )) &&
    cat $log >$log.$((++run)) && >$log
done
 
Hi,

When running the script I get the following error;

./awk.sh[3]: ++run: bad number

Any Ideas?
 
A 'run=0' before the loop might help that. Else your shell doesnt seem to support $(( expr )), should work with ba/k/zsh, does not with t/csh.

You could do run=`expr $run + 1` in the loop and use $run instead.
 
To test the scenario I mention, rename the file. If it continues logging to the renamed file, then the application is maintaining the open file handle.
 
Hi Eric,

What you describe is correct - when I rename the file, the file keeps logging...

What is the next idea?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top