简体   繁体   English

如何在Linux中使用crontab删除某些文件

[英]How to delete some file with crontab in linux

I have two questions about using crontab file: 关于使用crontab文件,我有两个问题:
1.I am using a service. 1.我正在使用一项服务。 When it runs, a new log file created everyday in a log directory. 运行时,每天都会在日志目录中创建一个新的日志文件。 i want to delete all files that already exist greater 5 day in that log directory 我想删除该日志目录中已有5天以上的所有文件
2.I want to delete all the infomation that exist greater than 5 days in a log file( /var/log/syslog) 2.我要删除日志文件(/ var / log / syslog)中存在超过5天的所有信息

I don't know how to do that with crontab in linux. 我不知道如何在Linux中使用crontab做到这一点。 Please help me! 请帮我! Thanks in advance! 提前致谢!

  1. If you are using logrotate for log rotation then it has options to remove old files, if not you could run something as simple as this once a day in your cron: 如果您使用logrotate进行日志轮换,则它具有删除旧文件的选项,如果没有,则可以每天在cron中运行像这样简单的操作:

find /path/to/log/folder -mtime +5 -type f -exec rm {} \\; 找到/ path / to / log / folder -mtime +5 -type f -exec rm {} \\;

Or more specific match a pattern in the filename 或者更具体地匹配文件名中的模式

find . 找 。 -mtime +5 -type f -name *.log -exec ls -l {} \\; -mtime +5 -type f -name * .log -exec ls -l {} \\;

  1. Why not set up logrotate for syslog to rotate daily then use its options to remove anything older than 5 days. 为什么不设置logrotate以使syslog每天轮换一次,然后使用其选项删除5天以上的任何东西。 Other options involve parsing log file and keeping certain aspect etc removing other bits etc which involved writing to another file and back etc and when it comes to live log files this can end up causing other issues such as a requirement to restart service to relog back into files. 其他选项涉及解析日志文件并保留某些方面,例如删除其他涉及写入另一个文件并返回等的位,当涉及到实时日志文件时,这可能最终导致其他问题,例如要求重新启动服务以重新登录到文件。 so best option would be logrotate for the syslog 所以最好的选择是syslog的logrotate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM