In Linux every activity is monitored and logged in to their respective logs in /var/log folder. The depth of log information is dependent on the log configuration we did it in /etc/logrotate.d and we have to rotate them all the time to control how much space they use. When ever we install packages, the package copies it’s log configuration file to /etc/logrotate.d folder so that that specific application log files can be rotated.
Normally in any Linux/Unix machine logs are rotated daily(/etc/cron.daily/) or weekly(/etc/cron.weekly) or monthly(/etc/cron.monthly) depending where we kept our logrotate script. My logrotate script is located in /etc/cron.daily which indicate my logs are rotated daily. One more facter logrotate depends when rotatting logs is log “status” file which is located at /var/lib/logrotate/status which stores when we rotated logs last time.
Let us see how we can rotate logs manually instead of waiting for hours.
Example1: In order to check what happens when we rotate log files manually before actually executing them can be done in debug mode(-d).
logrotate -d log-config-file
Example output:
root@linuxnix:/# logrotate -d libvirtd reading config file libvirtd Handling 1 logs rotating pattern: /var/log/libvirt/libvirtd.log weekly (4 rotations) empty log files are rotated, only log files >= 102400 bytes are rotated, old logs are removed considering log /var/log/libvirt/libvirtd.log log does not need rotating
Actually this will not rotate any logs, if you want to rotate logs manually we have use force on it.
Example2: Rotate logs for a specific application, for example here I taken my libvirt application logs
logrotate -f log-config-file
My libvirt folder size before log rotating them.
du -hs /var/log/libvirt/ 216K /var/log/libvirt/
Example:
logrotate -f /etc/logrotate.d/libvirtd
After executing above command my libvirt log folder size is
root@linuxnix:/# du -hs /var/log/libvirt/ 172K /var/log/libvirt/
Example3: How about rotating all the logs in your machine which keep their log configurations in /etc/logrotate.d folder
logrotate -f /etc/logrotate.conf
My /var/log folder size before execution is 15MB
Example: root@linuxnix:/# du -hs /var/log/ 15M /var/log/ root@linuxnix:/# logrotate -f /etc/logrotate.conf
My /var/log folder size is reduced to 13MB after this command execution
Output:
root@linuxnix:/# du -hs /var/log/ 13M /var/log/
Example4: With -f option we can not see what is happening background, if we want to see we can use verbose mode with -v.
logrotate -vf /etc/logrotate.conf
Clipped output:
reading config file /etc/logrotate.conf including /etc/logrotate.d reading config file apache2 reading config file apport reading config file apt reading config file consolekit reading config file cups-daemon reading config file dpkg reading config file glusterfs-common reading config file libvirtd reading config file libvirtd.lxc reading config file libvirtd.qemu reading config file libvirtd.uml reading config file pm-utils reading config file ppp reading config file rsyslog reading config file speech-dispatcher reading config file ufw reading config file unattended-upgrades reading config file upstart Handling 24 logs rotating pattern: /var/log/apache2/*.log forced from command line (14 rotations) empty log files are not rotated, old logs are removed switching euid to 0 and egid to 103 considering log /var/log/apache2/access.log log does not need rotating considering log /var/log/apache2/error.log log needs rotating considering log /var/log/apache2/other_vhosts_access.log log does not need rotating rotating log /var/log/apache2/error.log, log->rotateCount is 14 dateext suffix '-20150820' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' compressing log with: /bin/gzip switching uid to 0 and gid to 103
Hope this helps some one to logrotate their log files manually
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018