Is it possible to run one iteration of logrotate manually without scheduling it on some interval?
-
4The problem with logrotate is that it has one Global configuration file, and it does NOT let you run a single log rotate sub-configuration file while still applying the options set in the global configuration file. That is it does not have a nice way to run just ONE log rotation file check, exactly as it would when run each night from cron. It could do with a 'limit to these log files' option, or better still, 'run the global config, but only include this sub-configuration file'.– anthonyNov 30, 2016 at 2:27
8 Answers
Yes: logrotate --force $CONFIG_FILE
-
137note that
--force
will rotate file(s) even if they do not meet the specified criteria such as minsize, age, etc.– xoferJun 5, 2013 at 21:28 -
4If you place the logrotate config file in /etc/logrotate.d/custom.conf does this mean, you don't need to specify a size/time when the log should automatically rotate? Or should you place the config in a different folder if you do not intend to make it rotate automatically? Aug 7, 2013 at 9:19
-
24The criteria "notifempty" is not ignore by "--force". This stumped me for a while, so i mention this as an exception to @xofer statement.– thelogixSep 29, 2015 at 13:43
-
21You can just rotate all with
logrotate --force /etc/logrotate.d/
(just directory name)– fcmFeb 26, 2016 at 23:05 -
4It's true that
--force
will force rotation even if files do not meet criteria (age, size, etc), but please consider that this is the only way to spot real problems that withlogrotate -d
would not emerge (for example, I had a server running out of space due to logrotate not running for months...and thanks to--force
I figured out that there wereFile exists
errors. I manually deleted those files and now rotation correctly works again! Sep 10, 2018 at 8:41
logrotate -d [your_config_file]
invokes debug mode, giving you a verbose description of what would happen, but leaving the log files untouched.
-
108those who came here looking for the answer of a question like "how can i test my logrotate conf?" can take this one as an answer , as it just TESTS the conf Mar 29, 2014 at 20:50
-
not wroked with your approach, I tried with
logrotate --force
and works well. May 20, 2018 at 6:48 -
1I came here searching for an answer to why my logs were not rotated. This was the perfect answer for me, even if it was not the exact answer to the original question. Sep 28, 2020 at 9:29
-
In case you want to run and actually make the changes, run
logrotate -v <your_config_file>
where -v is to verbose the logs on the screen. Sep 27, 2021 at 6:57 -
1Guys, this doesn't return non-zero status code if there are errors in your config .– Ali TouOct 16, 2021 at 15:13
If you want to force-run a single specific directory or daemon's log files, you can usually find the configuration in /etc/logrotate.d
, and they will work standalone.
Keep in mind that global configuration specified in /etc/logrotate.conf
will not apply, so if you do this you should ensure you specify all the options you want in the /etc/logrotate.d/[servicename]
config file specifically.
You can try it out with -d
to see what would happen:
logrotate -df /etc/logrotate.d/nginx
Then you can run (using nginx as an example):
logrotate -f /etc/logrotate.d/nginx
And the nginx logs alone will be rotated.
-
2This is well and good, but you lose any global settings that you have in the "/etc/logrotate.conf". I wrote a script to merge the global and specific logrotate files together then use it so as to solve this problem. (see my answer).– anthonyJul 1, 2019 at 3:09
-
Using
logrotate -d /etc/logrotate.conf
will run through any other included conf files too. Jul 2, 2019 at 15:19
You may want to run it in verbose + force mode.
logrotate -vf /etc/logrotate.conf
The way to run all of logrotate is:
logrotate -f /etc/logrotate.conf
that will run the primary logrotate file, which includes the other logrotate configurations as well
Issue the following command,the way to run specified logrotate:
logrotate -vf /etc/logrotate.d/custom
Options:
-v :show the process
-f :forcing run
custom :user-defined log setting
eg: mongodb-log
# mongodb-log rotate
/data/var/log/mongodb/mongod.log {
daily
dateext
rotate 30
copytruncate
missingok
}
-
plus1 for actually providing a complete example. PS How can we determine what logrotate actually does to the files? Oct 5, 2017 at 17:17
Edit /var/lib/logrotate.status (or /var/lib/loglogrotate/logrotate.status) to reset the 'last rotated' date on the log file you want to test.
Then run logrotate YOUR_CONFIG_FILE
.
Or you can use the --force flag, but editing logrotate.status gives you more precision over what does and doesn't get rotated.
-
-
Try /var/lib/logrotate/status. I'm not sure if this is an debian/redhat thing, or if I put "." where I should have put "/". Dec 23, 2018 at 4:11
Created a shell script to solve the problem.
https://antofthy.gitlab.io/software/#logrotate_one
This script will run just the single logrotate sub-configuration file found in "/etc/logrotate.d", but include the global settings from in the global configuration file "/etc/logrotate.conf". You can also use other otpions for testing it...
For example...
logrotate_one -d syslog