简体   繁体   English

使用 cron 作业删除日志文件

[英]Remove log files using cron job

Hi.你好。 I want to remove all log files from the last 7 days from a folder, but leave all the other files.我想从文件夹中删除过去 7 天的所有日志文件,但保留所有其他文件。 Can I use the below command?我可以使用下面的命令吗? How do you specify that it just delete the files with .log extension?您如何指定它只删除扩展名为 .log 的文件?

 find  /path/to/file -mtime +7 -exec rm -f {} \; 

Do I need to write this command into some file, or can I just write it in command prompt and have it run automatically every day?我是否需要将此命令写入某个文件,或者我可以将其写入命令提示符并让它每天自动运行?

I have no idea how to run a cron job in linux.我不知道如何在 linux 中运行 cron 作业。

Use wildcard.使用通配符。 And just put it in your crontab use the crontab -e option to edit your crontab jobs.只需将其放入您的crontab中,使用crontab -e选项来编辑您的 crontab 作业。
See example:参见示例:

* * * * *  find  /path/to/*.log -mtime +7 -exec rm -f {} \; 

Just to increment the answer check this nice article on how to work with your crontab !只是为了增加答案,请查看这篇关于如何使用crontab的好文章! in Linux .在 Linux 中。

You edit your personal crontab by running crontab -e .您可以通过运行crontab -e来编辑您的个人crontab。 This gets saved to /var/spool/cron/<username> .这将保存到/var/spool/cron/<username> The file will be the owners username, so root would be /var/spool/cron/root.该文件将是所有者的用户名,因此 root 将是 /var/spool/cron/root。 Everything in the file is run as the owner of the file.文件中的所有内容都作为文件的所有者运行。

The syntax for crontab is as follows: crontab 的语法如下:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

When you are editing your own personal crontab, via crontab -e , you leave out the user-name field, because the user is inferred by the filename (see first paragraph).当你通过crontab -e编辑你自己的个人crontab 时,你会省略用户名字段,因为用户是由文件名推断出来的(见第一段)。

That being said, your entry should look like this:话虽如此,您的条目应如下所示:

0 5 * * *  find  /path/to/*.log -mtime +7 -delete

This will run every day, at 5:00 AM, system time.这将在系统时间每天早上 5:00 运行。 I don't think you need it to run any more frequently than daily, given the fact that you are removing files that are 7 days old.我认为您不需要比每天更频繁地运行它,因为您要删除 7前的文件。

Please don't use over use the -exec option, when the -delete option does exactly what you want to do.-delete选项完全符合您的要求时,请不要过度使用-exec选项。 The exec forks a shell for every file, and is excessively wasteful on system resources. exec 为每个文件派生一个 shell,并且过度浪费系统资源。

When you are done, you can use crontab -l to list your personal crontab.完成后,您可以使用crontab -l列出您的个人 crontab。

ps.附言。 The default editor on most Linux systems is vi, if you do not know vi, use something simple like nano by setting your environ variable export EDITOR=nano大多数 Linux 系统上的默认编辑器是 vi,如果你不知道 vi,可以通过设置环境变量export EDITOR=nano

find /path/to/dir-containing-files -name '*.log' -mtime +7 -exec rm -f {} \;

To create a cron job, put a file containing the following in the /etc/cron.daily dir:要创建 cron 作业,请将包含以下内容的文件放在 /etc/cron.daily 目录中:

#!/bin/sh
find /path/to/dir-containing-files -name '*.log' -mtime +7 -exec rm -f {} \;

You should use crontab -e to edit your crontab and schedule the job.您应该使用crontab -e来编辑您的 crontab 并安排作业。 It might look something like this:它可能看起来像这样:

* 1 * * * /usr/bin/find /path/to/file -name '*.log' -mtime +7 -exec rm -f {} \; 

This will recursively remove all .log files in the directory /path/to/file every day at 1am.这将在每天凌晨 1 点递归删除目录 /path/to/file 中的所有 .log 文件。

Since this is about log files, you should look at logrotate .由于这是关于日志文件,您应该查看logrotate It runs daily from system cron job and will rotate logs for you based on rules from /etc/logrotate.conf file, which usually includes /etc/logrotate.d directory.它每天从系统 cron 作业运行,并将根据 /etc/logrotate.conf 文件中的规则为您轮换日志,该文件通常包括 /etc/logrotate.d 目录。 So no need for crontab nor find.所以不需要 crontab 也不需要 find。

You can also have your own cron job if you have no access to add file to /etc/logrotate.d for your own configuration.如果您无权将文件添加到 /etc/logrotate.d 以进行自己的配置,您也可以拥有自己的 cron 作业。

There are plenty of examples in /etc/logrotate.d. /etc/logrotate.d 中有很多例子。

It expects your application to write to single file.它希望您的应用程序写入单个文件。 It is not for an application that logs into different log file each day.它不适用于每天登录到不同日志文件的应用程序。 An application generally needs not do that.应用程序通常不需要这样做。 If the application keeps the log file open, logrotate can run a postrotate script to tell the application to reopen the log file.如果应用程序保持日志文件打开,logrotate 可以运行 postrotate 脚本来告诉应用程序重新打开日志文件。

You guys are doing it the HARD way.你们正在以艰难的方式做到这一点。 Try using the clear command尝试使用 clear 命令

* * * * 0 clear > /home/user/CronLog.txt:

where 0 is Sunday and 7 would be Saturday.其中 0 是星期日,7 是星期六。 the ">" will clear the log as appose to ">>" which adds to the log. ">" 将清除日志,就像添加到日志中的 ">>" 一样。 If your log file is root then type in "root" before "clear" like this如果您的日志文件是 root,则在“clear”之前输入“root”,如下所示

* * * * 0 root clear > /home/user/CronLog.txt

After googling around on this particular topic, I found that many people recommend using the -delete option like so:在搜索了这个特定主题之后,我发现很多人建议使用-delete选项,如下所示:

* * * * *  find  /path/to/*.log -mtime +7 -delete;  

The benefits of this version is that it is easy to remember and it will perform better since -exec will spawn a new process for every file that is to be deleted.这个版本的好处是它很容易记住并且性能会更好,因为-exec将为每个要删除的文件生成一个新进程。

Here are some references: https://linuxaria.com/howto/linux-shell-how-to-use-the-exec-option-in-find-with-examples以下是一些参考资料: https ://linuxaria.com/howto/linux-shell-how-to-use-the-exec-option-in-find-with-examples
https://unix.stackexchange.com/questions/167823/find-exec-rm-vs-delete https://unix.stackexchange.com/questions/167823/find-exec-rm-vs-delete

This will delete log files older than 7 Days这将删除超过 7 天的日志文件

* * * * *  find  /path/to -name '*.log' -mtime +7 -exec rm -f {} \;

This will delete log files older than 30 Minutes这将删除超过 30 分钟的日志文件

* * * * *  find  /path/to -name '*.log' -mmin +30 -exec rm -f {} \;

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

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