1

We have our application server(tomcat) creating the catalina.log and we need to write a cron job(bash script) to run every hour and rotate the log files with timestamp.

currently we are doing this in the following way.

  1. copy the catalina.out to catalina.out.
  2. empty the catalina.out

The issue we have is, if anything written to the catalina.out during this process, that is gone missing during the emptying the catalina.out. Is there anyway to overcome this issue?

Thanks in advance Mayuran

2
  • Why don't you use "logrotate" command? It does what you want. Commented Oct 31, 2014 at 17:16
  • OR may be try this. In your bash script, instead of copying the file (which might take microseconds/seconds), use mv command to move catalina.out to catalina.out.<$time variable here> && touch catalina.out Commented Oct 31, 2014 at 17:20

1 Answer 1

1

While you should probably be using the logrotate command to manage your log rotation, that advice doesn't directly answer your question.

I see two problems you are having. First you state:

  1. copy the catalina.out to catalina.out.

That doesn't seem correct. Perhaps its a typo? Why would you copy the file to itself?

Secondly, you shouldn't be copying these (potentially large) files around. All you need to do is rename them. So, a poor-man's log rotation (again, you should use logrotate) would be like this:

FILE=catalina.out
TIME=`date -u +%s`
if [ -e $FILE ] ; then
  mv ${FILE} ${FILE}.${TIME} && touch ${FILE}
else
  touch ${FILE}
fi

This will, of course, fill up your disk with catalina.out.* files eventually. You would need to enhance the script to deal with that, or just use logrotate.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.