Remove log files using cron job
Asked Answered
E

8

19

Hi. I want to remove all log files from the last 7 days from a folder, but leave all the other files. Can I use the below command? How do you specify that it just delete the files with .log extension?

 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.

Enervate answered 17/3, 2014 at 18:43 Comment(1)
just added a nice link to show you how to manage your crontab jobs !Maxa
M
24

Use wildcard. And just put it in your crontab use the crontab -e option to edit your crontab jobs.
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 ! in Linux .

Maxa answered 17/3, 2014 at 18:47 Comment(1)
You can also use -delete rather than "-exec rm -f {} \;" A bit easier to remember!Quill
W
18

You edit your personal crontab by running crontab -e. This gets saved to /var/spool/cron/<username>. The file will be the owners username, so root would be /var/spool/cron/root. Everything in the file is run as the owner of the file.

The syntax for crontab is as follows:

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).

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. 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.

Please don't use over use the -exec option, when the -delete option does exactly what you want to do. The exec forks a shell for every file, and is excessively wasteful on system resources.

When you are done, you can use crontab -l to list your personal 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

Wistrup answered 26/10, 2015 at 21:38 Comment(0)
L
6
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:

#!/bin/sh
find /path/to/dir-containing-files -name '*.log' -mtime +7 -exec rm -f {} \;
Luzon answered 17/3, 2014 at 18:47 Comment(0)
M
4

You should use crontab -e to edit your crontab and schedule the job. 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.

Malapropos answered 17/3, 2014 at 18:48 Comment(0)
H
2

Since this is about log files, you should look at 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. So no need for crontab nor 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.

There are plenty of examples in /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.

Hearth answered 17/3, 2014 at 19:22 Comment(1)
Logrotate is the tool of choice here. I tend to roll logs when a certain log file size is reached (and then gzip old logs). That way the number of files does depend on the log volume, and not on time.Jaleesa
S
1

You guys are doing it the HARD way. Try using the clear command

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

where 0 is Sunday and 7 would be Saturday. 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

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

Sports answered 17/1, 2020 at 3:8 Comment(1)
This absolutely does not answer the question. Removing all log files that are older than 7 days is vastly different from removing a log file on the 7th day.Proteus
B
0

After googling around on this particular topic, I found that many people recommend using the -delete option like so:

* * * * *  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.

Here are some references: 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

Ballata answered 22/5, 2019 at 6:14 Comment(0)
S
0

This will delete log files older than 7 Days

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

This will delete log files older than 30 Minutes

* * * * *  find  /path/to -name '*.log' -mmin +30 -exec rm -f {} \;
Subbase answered 10/3, 2020 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.