This article is mainly about the use of the regular task of Linux to clean up 45 days before the weekly log. The server generates a large log file on a daily basis, so that the log files need to be cleaned up periodically in order to keep the hard disk from being stuffed with log files. At this point we can write a shell script to clean up the log 45 days before a certain path, and then set a scheduled task to execute the script regularly every week.
① to clean up log scripts under a path deloldlogs.sh:
[root@prx01 cleanlog]# vim/usr/local/cleanlog/deloldlogs.sh
#!/bin/sh
#删除输入路径下的修改时间在45天以前的日志文件 Find
$ 1-mtime +45-name "*log*"-exec rm-f {} \;
This code is not difficult to understand, is to clean up the path of parameter 1 under the modified time of 45 days before the log file
② the script that specifies which paths to clean up the log del_all_oldlogs.sh:
[root@prx01 cleanlog]# vim/usr/local/cleanlog/del_all_oldlogs.sh
#!/bin/bash
/usr/local/cleanlog/ Deloldlogs.sh "/home/usr/ewp/logs"
/usr/local/cleanlog/deloldlogs.sh "/home/usr/h5/logs"
/usr/local/ Cleanlog/deloldlogs.sh "/home/usr/payment/logs"
③ Add execute permissions to the script:
[root@prx01 cleanlog]# chmod a+x/usr/local/cleanlog/del*.sh
④ Add timed Task:
[ROOT@PRX01 cron]# Vim/var/spool/cron/root
Add the following:
0 * * 6/usr/local/cleanlog/del_all_oldlogs.sh
Note:
- To prevent a lack of permissions, a root user's timed task is set here
- This code means: Execute/usr/local/cleanlog/del_all_oldlogs.sh this script at 6 0:10 a week
⑤ View timed tasks:
[Root@app05 logs]# Crontab-l
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.