Wednesday, September 3, 2025

Linux Tips - Manage log files in Liunx/Unix Using /usr/sbin/logrotate

Unix / Linux provide command  "/usr/sbin/logrotate"  to rotate log files. 

Linux / Unix widely use this command to manage log files , we can use this command to manage our logs

Example I need to keep 7 day log , and after 7th day , it must over-write

I need to create conf file say /home/virag/TSautomanage.conf

/home/virag/*.log {

        rotate 7       
        daily
        missingok
        notifempty
        delaycompress
        compress
        postrotate
        mailx -s "rotation complete " virag123@gmail.com  < /home/virag/TSautomanage_boat >/dev/null
        endscript
}

Note  - "delaycompress" meaning the most recent file will not be compressed until the next rotation cycle.

Now run following command from cron / EM Metric Extension (ME) /OCI ME script/shell script /perl script

# logrotate -d /home/virag/TSautomanage.conf


it create log file like this

-rw-r----- 1 oracle adm  13629 Aug 22 17:55 TSautomanage.log
-rw-r----- 1 oracle adm 283548 Aug 21 07:35 TSautomanage.log.1  <== not compressed
-rw-r----- 1 oracle adm  11316 Aug 13 07:35 TSautomanage.log.2.gz
-rw-r----- 1 oracle adm  11591 Aug  7 06:43 TSautomanage.log.3.gz
-rw-r----- 1 oracle adm  14536 Jul 24 07:35 TSautomanage.log.4.gz


Here is another option to rotate based on size and keep for 90 days

$ cat logrotate.conf
/../../../alertSID.log {
        size 1G
        copytruncate
        rotate 12
        compress
        maxage 90
        dateext
        dateformat -%d%m%Y
        notifempty
        mail Virag123@gmail.com
}

Above example indicates that the rotated log files would be removed after 90 days.
Many more option available with /usr/sbin/logrotate

You can check cong file created for Unix/Linux system as an example
[root@Myatmw1 ~]# ls -ltr /etc/logrotate.d
total 68
-rw-r--r-- 1 root root 106 Jul 23  2015 numad
-rw-rw-r-- 1 root root 135 May 11  2016 ConsoleKit
-rw-r--r-- 1 root root 155 Jan 11  2017 aide
-rw-r--r-- 1 root root  87 Jan 12  2017 yum
-rw-r--r-- 1 root root  32 Aug  9  2017 up2date
-rw-r--r-- 1 root root 172 Oct  6  2017 iscsiuiolog
-rw-r--r-- 1 root root 139 May  1  2018 dracut
-rw-r--r-- 1 root root 185 May  1  2018 httpd
-rw-r--r-- 1 root root 210 May  3  2018 syslog
-rw-r--r-- 1 root root 115 May  3  2018 samba
-rw-r--r-- 1 root root  94 Jun 13  2018 opensm
-rw-r--r-- 1 root root 115 Jul 25  2018 osad
-rw-r----- 1 root root 185 Nov 22  2018 hwmgmtd
-rw-rw-rw- 1 root root 101 Jan  5  2019 asmaudit

Also you can get more details using man command

 man logrotate

No comments:

Post a Comment

Google