Tomcat is a core project in the Jakarta Project of the Apache Software Foundation (Apache Software Foundation), developed jointly by Apache,sun and other companies and individuals. With Sun's involvement and support, the latest servlet and JSP specifications are always reflected in Tomcat, and Tomcat 5 supports the latest servlet 2.4 and JSP 2.0 specifications. Because of advanced Tomcat technology, stable performance, and free, so deeply loved by Java enthusiasts and has been recognized by some software developers, become the most popular Web application server.
Tomcat server is a free open source Web application server, belongs to the lightweight application server, in small and medium-sized systems and concurrent access users are not widely used, is the first choice to develop and debug JSP programs. As a scholar, it can be argued that when an Apache server is configured on a single machine, it can be used to respond to requests for access to HTML (an application under the standard Universal Markup Language) page. In fact, Tomcat is an extension of the Apache server, but it runs independently at runtime, so when you run Tomcat, it actually runs separately as a separate process from Apache.
The trick is that when configured correctly, Apache serves HTML pages, while Tomcat actually runs JSP pages and Servlets. In addition, like the Web server such as Tomcat and IIS, with the ability to handle HTML pages, it is also a servlet and JSP container, the standalone servlet container is the default mode of Tomcat. However, Tomcat's ability to handle static HTML is inferior to that of Apache servers. The latest version of Tomcat is now 9.0.
In many cases when we use Tomcat, the Tomcat console logs are stored under the log folder in Tomcat's installation directory, and the log file is named Catalina.out.
If this log file does not clean up, it will continue to grow, in my previous company, due to negligence, resulting in the production of this log grew to a good dozens of GB, with the error to look at the log, is not open at the time, the use of the SED command of Linux to intercept the log, but more trouble, Later after looking at the article on the Internet, found that this log can be segmented, so the following to explain how to cut the log, and with the script to automatically clean up.
Tomcat's log-splitting configuration
Upload the cronolog-1.6.2.tar.gz, to the Linux server/software/directory, the article involved in the package, you can download Baidu, or add the bottom, I send you free of charge you can also
#cd/software/
#tar-ZXVF cronolog-1.6.2.tar.gz
#cd cronolog-1.6.2
#./configure
#make
#make Install
After the installation. We use commands to view the command path
#which Cronolog
: Command path in:/usr/local/sbin/cronolog
Use a Tomcat user to modify a tomcat script if your tomat is started with root, then it is good to use root, but the production environment does not recommend the root user, there is a risk, just a suggestion.
Modify tomcat/bin/catalina.sh (for insurance purposes, back up before modifying)
#touch "$CATALINA _out"
if["$" = "-security"]; Then
If [$have _tty-eq 1]; Then
echo "Using Security Manager"
Fi
Shift
Eval "\" $_runjava\ "" \ "$LOGGING _config\" "$LOGGING _manager $JAVA _opts$catalina_opts \
-Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\""-classpath "\"$CLASSPATH\"" -Djava.security.manager -Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" -Dcatalina.base="\"$CATALINA_BASE\"" -Dcatalina.home="\"$CATALINA_HOME\"" -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" org.apache.catalina.startup.Bootstrap“$ @”start 2>&1 \#这行是新添加的 | / usr / local / sbin / cronolog“$ CATALINA_BASE”/logs/catalina.%Y-%m-%d.out >> / dev / null&#这行是新添加的
#org.apache.catalina.startup.bootstrap "$ @" Start \# this line was originally, commented out
#>> "$ catalina_out" 2>&1 "&" # This line turns out to be commented out
else eval“\”$ _ RUNJAVA \“”“\”$ LOGGING_CONFIG \“”$ LOGGING_MANAGER $ JAVA_OPTS $ CATALINA_OPTS -Djava.endorsed.dirs =“\”$ JAVA_ENDORSED_DIRS \“” - classpath“\”$ CLASSPATH \“” -Dcatalina.base =“\”$ CATALINA_BASE \“” -Dcatalina.home =“\”$ CATALINA_HOME \“” -Djava.io.tmpdir =“\”$ CATALINA_TMPDIR \“” org.apache.catalina.startup.Bootstrap“$ @”start 2>&1 \#这行是新添加的 | / usr / local / sbin / cronolog“$ CATALINA_BASE”/logs/catalina.%Y-%m-%d.out >> / dev / null& #这行是新添加的
#org.apache.catalina.startup.bootstrap "$ @" Start \# this line was originally, commented out
#>> "$ catalina_out" 2>&1 "&" # This line turns out to be, commented out
When you have finished modifying the Tomat, you will generate a question that is suffixed with the date of the day, as shown in:
After the log cut is complete, we can do the log cleanup below is the log cleanup script
#!/bin/bash
#clear Tomcat LOGSS
Domain_home = "/App/tomcat" # defines the path where Tomcat is located
LogFileName = Catalina. date +%Y-%m-%d -d‘-5 days‘
Out # processing How many days before the Tomcat log can be customized
CD $ {domain_home}/logs
If [-F $ {Domain_home}/logs/catalina. date +%Y-%m-%d -d‘-1 days‘
Out] # compress how many days before the log
Then
If [-F $ {domain_home}/logs/${logfilename}]
The
/USR/BIN/BZIP2 ${domain_home}/logs/${logfilename}
/usr/bin/find. -name "*"-type f-mtime +21-exec rm-rf {} \; # delete files from 21 days ago
Fi
Fi
Exit 0
After the script is defined, we put the script in the/root/scripts/directory on the server directory, called clear_tomcat.sh
Copy the contents of the script into the clear_tomcat.sh file, using the command: chmod 755 clear_tomcat.sh to execute permissions
And then, we define a scheduled task.
#crontab-E
Add the following line
*/bin/sh/root/scripts/clear_tomcat.sh >>/dev/null&
And then save the exit is OK, the above means that every night 10:30 to execute the script, please the Crond service is running, the script will be able to run normally
In this case a file a day, if an error occurs in the background log to look for some, and do not need to worry about the log file has been growing problems.
If you have any questions about this article, please add the following discussion
Linux/centos Tomcat Configuration Log slicing and automatic script cleanup