A Tomcat startup script that deletes old log files before starting up

One thing I don't like about using Tomcat in development and test environments is that the log files tend to grow, eventually growing for days on end if you don't clean them out. Having all these old log files hanging around just makes it harder to debug development problems, so I like to delete them all every time I restart Tomcat.

To solve the problem of these growing log files in dev/test environments, I wrote my own "wrapper" shell script that I call to start my Tomcat server in development and test environments. Instead of running the startup.sh script that is provided with the Tomcat distribution, I run my shell script, which first cleans out all the old log files, and then starts Tomcat with the startup.sh shell script.

Here's the source code for my shell script:

#!/bin/sh

echo "DELETING OLD LOG FILES ..."
cd ../logs
rm catalina*
rm local*
rm manager*
rm admin*
rm host*

echo "DOING STARTUP ..."
cd ../bin
./startup.sh

What this script does

I just put this shell script in the Tomcat bin directory and call it something like al.sh (named after me :), and run it instead of the startup.sh script.

As you can see, my startup script does two things:

  1. Moves to the Tomcat logs directory, and deletes all the log files there.
  2. Moves back to the Tomcat bin directory, and runs the Tomcat startup.sh script.

I like this, because then, when I'm debugging a problem by digging through the log files, I know that I'm just looking at the log files since the most recent Tomcat server restart.

A slightly longer script

I sometimes use a slightly longer version of this script where I also delete the latest version of my application directory. Frankly, I can't remember why I used to do this ... I know it was due to a problem where my web application changes weren't being deployed properly, but I haven't used this version of the script in a while, so maybe that was an old Tomcat deployment problem.

Hmm, well ... I still don't remember why I did this, but I thought I'd share this version of the script here as well. As you can see from the code, the only difference is that this script moves into Tomcat's webapp directory, and then removes the directory for the TestWebService, which was the name of the last web application I was working with. Just change that directory name to the directory corresponding to the web application you want to delete when Tomcat is restart, and you're ready to go.

#!/bin/sh

echo "DELETING OLD LOG FILES ..."
cd ../logs
rm catalina*
rm local*
rm manager*
rm admin*
rm host*

echo "DELETING OLD WEB APP ..."
cd ../webapps
rm -r TestWebService

echo "DOING STARTUP ..."
cd ../bin
./startup.sh