Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logrotate does not auto rotate when based on log size

Tags:

logrotate

I have a custom application (myApp) which is writing logs to the file '/var/log/myApp'. I can see the logs being written and it works fine. Now I am trying to setup logrotate for this file and for this I have created a config file '/etc/logrotate.d/myApp', the contents of which are -

/var/log/myApp {
   missingok
   size +10k
   start 0
   nocompress
   create 0600 root root
   rotate 10
   postrotate
     /etc/init.d/rsyslog restart > /dev/null 2>&1 || true
   endscript
}

Now if i do a logrotate -dv /etc/logrotate.d/myApp I don't see any errors as such and when logrotate -f /etc/logrotate.d/myApp is executed i.e., a forceful logrotate the log is rotated. But when the log file size exceeds 10k the log is not automatically rotated. Any help would be appreciated.

like image 807
One Avatar asked Jun 30 '11 19:06

One


People also ask

How do you rotate logs with logrotate?

Handling Active Log File. In the default behavior, the rotation process beings by logrotate renaming the active log file into a different name. Then, it creates a new log file with the same name. Finally, it invokes other logic such as compress and mail to complete the rotation process.

How often does logrotate check size?

Normally, logrotate is run as a daily cron job. It will not modify a log more than once in one day unless the criterion for that log is based on the log's size and logrotate is being run more than once each day, or unless the -f or --force option is used. Any number of config files may be given on the command line.

How do you force a log to rotate?

If you want to force Logrotate to rotate the log file when it otherwise would not have, use the --force flag: logrotate /home/sammy/logrotate. conf --state /home/sammy/logrotate-state --verbose --force.

How do I force rotate var log messages?

If you want to rotate /var/log/syslog it needs to be listed in a logrotate config file somewhere, and you just run logrotate . If it rotated recently, then logrotate -f to force it to do it again. So, you need that in a file, normally either /etc/logrotate. conf or as a file snippet in /etc/logrotate.


2 Answers

logrotate rotates the logs specified in /etc/logrotate.d/ according to the time interval configured in /etc/logrotate.conf. On some distros, the default value is a week. You can override that time interval in your specific config using e.g. 'daily' in your config file.

Log files will not be rotated until logrotate has known about the files for at least as long as the time specified. I assume that you waited this long and/or modified the conf file?

like image 164
FuriousGeorge Avatar answered Oct 23 '22 04:10

FuriousGeorge


logrotate is working only once a day , So it's not checking your file size all the time.

if you want an effective way to keep the size lower , you should use a cron job like

for example , force the rotate every hour in your crontab

0 * * * * logrotate <my conf here> 

and your logs will be , if needed, rotated every hour. I had such problem in my free-tier aws server , as the size exceed rapidly and i didn't have enough disk space to wait 24 hours for a rotate

like image 27
kommradHomer Avatar answered Oct 23 '22 02:10

kommradHomer