657

Is it possible to run one iteration of logrotate manually without scheduling it on some interval?

Vladimir
  • 12,753
  • 19
  • 62
  • 77
  • 4
    The 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'. – anthony Nov 30 '16 at 02:27

8 Answers8

866

Yes: logrotate --force $CONFIG_FILE

pilcrow
  • 56,591
  • 13
  • 94
  • 135
  • 138
    note that `--force` will rotate file(s) even if they do not meet the specified criteria such as minsize, age, etc. – xofer Jun 05 '13 at 21:28
  • 4
    If 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? – Damainman Aug 07 '13 at 09:19
  • 25
    The criteria "notifempty" is not ignore by "--force". This stumped me for a while, so i mention this as an exception to @xofer statement. – thelogix Sep 29 '15 at 13:43
  • It means it succeed if it doesn't print anything in the console when I run it, right? – Aminah Nuraini Feb 14 '16 at 21:48
  • 21
    You can just rotate all with `logrotate --force /etc/logrotate.d/` (just directory name) – fcm Feb 26 '16 at 23:05
  • 4
    It'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 with `logrotate -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 were `File exists` errors. I manually deleted those files and now rotation correctly works again! – lucaferrario Sep 10 '18 at 08:41
  • 1
    `$CONFIG_FILE` has to include all the config. As opposed to doing a 'sub' config file - for example the ones you can commonly find in `/etc/logrotated.d/` – Chris Stryczynski Feb 15 '21 at 13:40
  • As rightly mentioned in the above comments **--force** will rotate the logs even if does NOT meet the specific criteria. To actually rotate as per the exact criteria in the config file, run `logrotate -v $CONFIG_FILE` **-v** to print the verbose logs to the screen. – Vishwas M.R Sep 27 '21 at 06:54
686

logrotate -d [your_config_file] invokes debug mode, giving you a verbose description of what would happen, but leaving the log files untouched.

AKHolland
  • 4,435
  • 23
  • 35
sandover
  • 7,488
  • 1
  • 18
  • 11
  • 117
    those 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 – kommradHomer Mar 29 '14 at 20:50
  • not wroked with your approach, I tried with `logrotate --force` and works well. – Benyamin Jafari May 20 '18 at 06:48
  • 1
    I 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. – Erik Melkersson Sep 28 '20 at 09:29
  • In case you want to run and actually make the changes, run `logrotate -v ` where **-v** is to verbose the logs on the screen. – Vishwas M.R Sep 27 '21 at 06:57
  • 2
    Guys, this doesn't return non-zero status code if there are errors in your config . – Ali Tou Oct 16 '21 at 15:13
145

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.

trisweb
  • 8,814
  • 3
  • 32
  • 22
  • 2
    This 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). – anthony Jul 01 '19 at 03:09
  • Using `logrotate -d /etc/logrotate.conf` will run through any other included conf files too. – wmassingham Jul 02 '19 at 15:19
70

You may want to run it in verbose + force mode.

logrotate -vf /etc/logrotate.conf

Sohail Ahmed
  • 1,047
  • 1
  • 11
  • 27
vivekv
  • 2,238
  • 3
  • 23
  • 37
42

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

Allen Hancock
  • 579
  • 4
  • 6
29

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
}
Swetha
  • 746
  • 10
  • 19
wangc
  • 299
  • 3
  • 4
  • plus1 for actually providing a complete example. PS How can we determine [what logrotate actually does to the files](https://stackoverflow.com/questions/46578307/)? – personal_cloud Oct 05 '17 at 17:17
11

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.

Ben Aveling
  • 842
  • 7
  • 13
6

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
anthony
  • 7,696
  • 1
  • 17
  • 11