Currently I am using a cron job to delete old files.
I like to know if logrotate
can do the same.
I do not need to want to rotate files. I just need to delete log files older thane 7 days.
I tried the following config file:
/opt/docker-volumes/syslog-ng/log/test/*.log
{
daily
rotate 1
maxage 0
missingok
ifempty
}
But it does not delete files.
To diagnose I did this:
when I run sudo logrotate -d /etc/logrotate.d/a2a_log_cleanup
I get this result
otating pattern: /opt/docker-volumes/syslog-ng/log/test/*.log
after 1 days (1 rotations)
empty log files are rotated, old logs are removed
considering log /opt/docker-volumes/syslog-ng/log/test/test1.log
Creating new state
Now: 2024-07-17 16:44
Last rotated at 2024-07-17 16:00
log does not need rotating (log has already been rotated)
considering log /opt/docker-volumes/syslog-ng/log/test/test2.log
Creating new state
Now: 2024-07-17 16:44
Last rotated at 2024-07-17 16:00
log does not need rotating (log has already been rotated)
considering log /opt/docker-volumes/syslog-ng/log/test/test3.log
Creating new state
Now: 2024-07-17 16:44
Last rotated at 2024-07-17 16:00
log does not need rotating (log has already been rotated)
In the directory, there are files older than one day:
ls -al
total 16
drwxr-xr-x 2 ali ali 4096 Jul 17 16:41 .
drwxr-xr-x 4 root root 4096 Jul 17 16:41 ..
-rw-r--r-- 1 root root 6 Jun 26 16:37 test1.log
-rw-r--r-- 1 root root 6 Jun 26 16:37 test2.log
-rw-r--r-- 1 root root 0 Jun 26 16:37 test3.log
Question:
Can I configure logrotate
to just delete old files and do not rotate them?
It looks like your program is creating all the separate (maybe daily) log files (the test1.log
, test2.log
, and test3.log
files) and you only want logrotate
to delete any older than 7 days.
You say that in your test, that "there are files older than one day". But logrotate
doesn't know that the very first time it runs. It stores all its known state about your logs in the file /var/lib/logrotate.status
. In your test, when you see the lines:
considering log /opt/docker-volumes/syslog-ng/log/test/test1.log
Creating new state
Now: 2024-07-17 16:44
Last rotated at 2024-07-17 16:00
log does not need rotating (log has already been rotated)
It's telling you that as far as logrotate
knows, that log file has never been dealt with before. It creates a new state for the log file based on the current time (not based on any timestamp on the file itself). As you can see from the code, newState
creates a record down to the hour, ignoring minutes and seconds. That's why it says the current time is 16:44
and the file was last rotated at 16:00
.
Since it hasn't reached a threshold to do anything yet, it doesn't do anything.
I'm also not sure what the behavior would be with:
rotate 1
maxage 0
Given the man page says:
maxage count
Remove rotated logs older than <count> days. The age is only checked if the logfile is to be rotated. The files are mailed to the configured address if maillast and mail are configured.
rotate count
Log files are rotated count times before being removed or mailed to the address specified in a mail directive. If count is 0, old versions are removed rather than rotated.
It sounds like it might rotate your logs once, and then delete them the next day (or maybe on the next run of logrotate
, I'm not sure how it deals with immediately created rotated logs). Either way, it seems like according to the docs, you would be better served by just using rotate 0
and no maxage
.
This would also eliminate the potential problem that any rotated log files would match your wildcard pattern and then be subject to their own rotation:
test1.log
would be rotated to test1.log.1
test2.log
would be rotated to test2.log.1
test3.log
would be rotated to test3.log.1
And then, on the next run of logrotate
, since these new files match your wildcard and have never been rotated before (there's no entry for them in the state file), they'll be subject to rotation:
test1.log.1
would be rotated to test1.log.1.1
test2.log.1
would be rotated to test2.log.1.1
test3.log.1
would be rotated to test3.log.1.1
As the man page states:
Please use wildcards with caution. If you specify *, logrotate will rotate all files, including previously rotated ones. A way around this is to use the olddir directive or a more exact wildcard (such as *.log).