Linux splits tomcat logs through crontab
Tomcat Catalina Log decisive time will be very large, can not be viewed, there are many kinds of log segmentation methods on the Web. This paper mainly studies the process of segmenting tomcat logs via Linux crontab timing tasks: First Prepare sh execution script
The following script is to split the Catalina.out log under Log_path, move the log to the Log_path/cron directory and rename it to Catalina.out. Current date. Log
#!/bin/sh
#需要进行分割log的路径, this script generally only modifies this path to use
log_path=/home/webapp/apache-tomcat-7.0.61/logs
#获取当前日期
d= ' Date +%y-%m-%d '
#获取30天前的日期
d90= ' date-d ' day ago ' +%y-%m-%d '
#cd到log_path路径下 and copy log files to the cron directory
CD ${log_path} && cp catalina.out $log _path/cron/catalina.out. $d. Log
#清空原日志文件
echo > Catalina.out
#移除30天前的日志文件
crontab timed Task
Save the script file as an. sh suffix file and place it on the Linux server. Can be the first SH run to see whether the error, if the report file cannot find such errors, first check the path is correct, if the path is not a problem, you need to check the SH file format, the following steps to check and set script file format. VI Command Open SH script run set FF see if the file format is UNIX if the file format is not UNIX, run set Ff=unix:wq save and exit
Re-run the sh script file without problems, configure the Crontab timed task, timed direct script files. CRONTAB-E open crontab Edit; Edit crontab execute split script every night 11:55
* * * * SH script directory/**.sh:wq save and exit. Crontab scheduled Task configuration completed. You can check whether the setting is successful by crontab-l
OK, so you can see whether the log is automatically cut and finished the next day.