1. this article is a simplified script for automatic system deployment. The main function is to clear logs from all directories. based on the given log directory, the deletion time is combined with crontab to clean up the log. After the cleaning is complete, generate a monthly cleanup log in/var/log/deltelog/. 3. because the script uses mtime (last modification time) to delete expired backups, it can be used to delete expired backups without the limitation of file names. shell tips
1. This article is a simplified script for automatic system deployment. The main function is to clear logs from all directories.
2. Based on the given log directory, the deletion time is combined with crontab to clear logs. After cleaning is complete, the cleanup logs by month are generated in/var/log/deltelog /.
3. Expansion. Because the script uses mtime (last modification time) for deletion, it can be used to delete expired backups, and is not limited by the file name.
Shell tips
Find? Type f? Print outputs the searched files based on line breaks or spaces. Different responses are made to different sh files. If xargs is used to delete and change files, it will be affected.
So you need to add? Print0 uses null as the boundary symbol to marry xargs? O to format the input
When using find, follow the minimum result set principle. The find analytical expression is left-to-right, so that all the filter symbols on the leftmost can filter the maximum data.
You can add it to crontab as needed.
Shell script
#!/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