1. A streamlined script for automating the deployment of systems, with the main function of cleaning up all directory logs
2. According to the given log directory, delete time combined with crontab to clean the log, clean up after completion, after the/var/log/deltelog/generated in the month of the cleanup log
3. Extension, because the script is Mtime (last modified time) to delete, so can be used to delete expired backups, and so on, not subject to file name restrictions
Shell Tips
Find–type F–print will be based on the line or space to output the search file, under different sh have different responses, if not done with Xargs to delete the change operation, will have an impact
So you need to add –print0 use NULL as a boundary symbol to get married. Xargs–o to format input
Use Find to follow the minimum result set principle, find analytic form left to right, all make sure that your leftmost filter symbol is able to filter the maximum data
You can add to the crontab according to your needs.
Shell Script
Copy Code code as follows:
#!/bin/sh
###########################
#delete Log blog.duplicatedcode.com
# in_day_num:like 1 2 is delete 2day ago logs
# In_log_path like Tomcat log home
###########################
In_log_path=${1}
IN_DAY_NUM=${2}
tmp_delete_log=/var/log/deletelog/"' Date +%y%m '. Log"
Deletelog ()
{
Inner_num=${1}
#find Log
echo "[' Date '] >> start delete logs---" >> $tmp _delete_log
Find ${in_log_path}-type f-mtime ${inner_num}-print0 | xargs-0 RM-RF
echo "[' Date '] >> end Delete Logs---" >> $tmp _delete_log
}
Init ()
{
Mkdir-p/var/log/deletelog/
}
Main ()
{
Init
If [-Z ${in_log_path}];then
echo "[' Date '] >> error Log_path not init---" >> $tmp _delete_log
Return
Fi
Inner_day_num=+7
if [[[N ${in_day_num}]]] && [[${in_day_num}-ge 1]]; Then
${inner_day_num}=${in_day_num}
Fi
Deletelog ${inner_day_num}
}
Main