Small bug of Logrotate

Source: Internet
Author: User
Tags file copy glob log log mkdir syslog
As we know, Logrotate is a program that manages the log. As for logrotate how to use, there are a lot of online here is not described.

Now talk about Logrotate bugs;

First give a file that can execute the logrotate command;

Function: Realize Aaaa.log and Bbbb.log copy to/var/log/test directory;

The following is the contents of the file: (the file name is test)

/var/log/aaaa.log/var/log/bbbb.log {
    sharedscripts
    prerotate
        /bin/mkdir-p/var/log/test
        /BIN/CP- f/var/log/aaaa.log/var/log/test/
        /bin/cp-f/var/log/bbbb.log/var/log/test/
    endscript

    postrotate
        echo "Restart Syslog"
        /bin/kill-hup ' cat/var/run/syslogd.pid 2>/dev/null ' 2>/dev/null | | true
        ECH O "End"
    endscript
}

Execute command: LOGROTATE-VF Test (here is the execution of plus f)

command Execution procedure:

[root@localhost logrotate.d]# logrotate-vf Test reading config file test handling 1 Logs rotating pattern:/var/log/aaa A.log/var/log/bbbb.log forced from command line (no old logs'll be kept) empty log files are rotated, old logs are REM Oved considering Log/var/log/aaaa.log log needs rotating considering Log/var/log/bbbb.log log needs rotating Rotatin G Log/var/log/aaaa.log, Log->rotatecount is 0 dateext suffix ' -20150311 ' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9 ][0-9][0-9] ' renaming/var/log/aaaa.log.1 to/var/log/aaaa.log.2 (rotatecount 1, Logstart 1, I 1), old LOG/VAR/LOG/AAAA.L Og.1 does not exist renaming/var/log/aaaa.log.0 To/var/log/aaaa.log.1 (Rotatecount 1, logstart 1, I 0), old Log/var/log /aaaa.log.0 does not exist log/var/log/aaaa.log.2 doesn ' t exist--won ' t try to dispose of it rotating log/var/log/bbbb.
Log, Log->rotatecount is 0 dateext suffix ' -20150311 ' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] ' Renaming/var/log/bbbb.log.1 to/Var/log/bbbb.log.2 (Rotatecount 1, Logstart 1, I 1), old Log/var/log/bbbb.log.1 does not exist Renaming/var/log/bbbb.log .0 To/var/log/bbbb.log.1 (Rotatecount 1, logstart 1, I 0), old log/var/log/bbbb.log.0 does not exist LOG/VAR/LOG/BBBB.L Og.2 doesn ' t exist--won ' t try to dispose of it running Prerotate script fscreate the context set to Unconfined_u:object_r:va  R_log_t:s0 renaming/var/log/aaaa.log to/var/log/aaaa.log.1 Disposename'll be/var/log/aaaa.log.1 fscreate context set To Unconfined_u:object_r:var_log_t:s0 Renaming/var/log/bbbb.log to/var/log/bbbb.log.1 Disposename'll be/var/log/ Bbbb.log.1 running Postrotate script Restart syslog end removing old log/var/log/aaaa.log.1 removing old log/var/log/bbb b.log.1 [Root@localhost logrotate.d]#

Execute command result:

[root@localhost log]# ls aaaa.log cron-20150215 prelink test Anaconda cron-20150222 
Restart Vboxadd-install.log Audit Dracut.log Samba Vboxadd-install-x11.log Bbbb.log GDM Secure VBoxGuestAdditions.log bittorrent lastlog sec ure-20150202 vboxguestadditions-uninstall.log boot.log Mail secure-20150209 wtmp boot.log-20 150202 maillog secure-20150215 wtmp-20141120 boot.log-20150209 maillog-20150202 secure-20150222 Xferl            OG boot.log-20150215 maillog-20150209 speech-dispatcher xorg.0.log boot.log-20150222 maillog-20150215 Spooler           Xorg.0.log.old btmp maillog-20150222 spooler-20150202 xorg.1.log btmp-20150302 spooler-20150209 xorg.9.log consolekit messages-20150309 spooler-20150215 yum.log cron NTP Stats spooler-20150222 cron-20150202 pm-powersave.log SSSD cron-20150209 PPP Tallylog [root@localhost log]# [ Root@localhost log]# [root@localhost log]# [root@localhost log]# [root@localhost log]#] [root@localhost log]# [         Root@localhost log]# ls Anaconda cron-20150222 prelink tallylog Audit Dracut.log           Restart test BitTorrent GDM Samba Vboxadd-install.log Boot.log    Lastlog secure vboxadd-install-x11.log boot.log-20150202 Mail secure-20150202 VBoxGuestAdditions.log boot.log-20150209 maillog secure-20150209 vboxguestadditions-uninstall.log boot.log -20150215 maillog-20150202 secure-20150215 wtmp boot.log-20150222 maillog-20150209 secure-20150222 wtmp-201411            Btmp maillog-20150215 speech-dispatcher xferlog btmp-20150302 maillog-20150222 Xorg.0.log Consolekit mesSages spooler-20150202 xorg.0.log.old cron messages-20150309 spooler-20150209 xorg.1.log cron -20150202 ntpstats spooler-20150215 xorg.9.log cron-20150209 pm-powersave.log spooler-20150222 y Um.log cron-20150215 PPP SSSD [root@localhost log]#
[root@localhost log]# ls test/
aaaa.log bbbb.log
[root@localhost log]#

You can see that Aaaa.log and Bbbb.log have been deleted and copied to the test directory.

The above is the normal result after the execution of Logrotate;

#############################################################################################################

#

#############################################################################################################

now is the point, let's talk about the unusual situation under logrotate.

The file that executes the logrotate command after the modification;

Function: Realize aaaa.log bbbb.log bbbb.log.1 file copy to/var/log/test directory;

The following is the contents of the file (this file name is Test1)

/var/log/aaaa.*/var/log/bbbb.* {
    sharedscripts
    prerotate
        /bin/mkdir-p/var/log/test/bin/cp-f
        / Var/log/aaaa.*/var/log/test/
        /bin/cp-f/var/log/bbbb.*/var/log/test/endscript postrotate
        echo " Restart Syslog "
        /bin/kill-hup ' cat/var/run/syslogd.pid 2>/dev/null ' 2>/dev/null | | true
        echo" End "
  
   endscript
}
  

Execute command: LOGROTATE-VF test1

To execute a command procedure:

[Root@localhost logrotate.d]# logrotate-vf test1 reading config file test1 handling 1 logs rotating pattern:/var/log/a Aaa.*/var/log/bbbb.* forced from command line (no old logs'll be kept) empty log files are rotated, old logs are Remov Ed considering Log/var/log/aaaa.log log needs rotating considering Log/var/log/bbbb.log log needs rotating Consideri ng Log/var/log/bbbb.log.1 Log needs rotating rotating log/var/log/aaaa.log, log->rotatecount is 0 dateext suffix '- 20150311 ' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] ' renaming/var/log/aaaa.log.1 to/var/log/ Aaaa.log.2 (Rotatecount 1, Logstart 1, I 1), old Log/var/log/aaaa.log.1 does not exist renaming/var/log/aaaa.log.0 Ar/log/aaaa.log.1 (Rotatecount 1, logstart 1, I 0), old log/var/log/aaaa.log.0 does not exist log/var/log/aaaa.log.2 Doe SN ' t exist--won ' t try to dispose of it rotating log/var/log/bbbb.log, log->rotatecount is 0 dateext suffix '-2015031 1 ' glob pattern '-[0-9][0-9][0-9][0-9][0-9] [0-9] [0-9] [0-9] ' Renaming/var/log/bbbb.log.1 to/var/log/bbbb.log.2 (rotatecount 1, Logstart 1, I 1), renaming/var/log/bbbb.log.0 To/var/log/bbbb.log.1 (Rotatecount 1, logstart 1, I 0), old log/var/log/bbbb.log.0 does not exist rotating Bbbb.log.1, Log->rotatecount is 0 dateext suffix ' -20150311 ' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] ' renaming/var/log/bbbb.log.1.1 to/var/log/bbbb.log.1.2 (rotatecount 1, Logstart 1, I 1), old log/var/log/bbbb.log.1.1 Does not exist renaming/var/log/bbbb.log.1.0 to/var/log/bbbb.log.1.1 (Rotatecount 1, logstart 1, I 0), old log/var/log/ 
bbbb.log.1.0 does not exist log/var/log/bbbb.log.1.2 doesn ' t exist--won ' t try to dispose of it running Prerotate script
Fscreate context set to Unconfined_u:object_r:var_log_t:s0 Renaming/var/log/aaaa.log to/var/log/aaaa.log.1 Disposename would be/var/log/aaaa.log.1 fscreate context set to Unconfined_u:object_r:var_log_t:s0 renaming/var/log/ Bbbb.log To/var/log/bbbb.log.1 Disposename would be/var/log/bbbb.log.1 fscreate context set to Unconfined_u:object_r:var_log_t:s0 renaming/var/log/b
Bbb.log.1 to/var/log/bbbb.log.1.1 Disposename'll be/var/log/bbbb.log.1.1 running postrotate script restart Syslog end Removing old log/var/log/aaaa.log.1 removing old log/var/log/bbbb.log.1 error:error opening/var/log/bbbb.log.1:no suc h file or directory [root@localhost logrotate.d]#

Execute command Result:

[root@localhost log]# ls aaaa.log cron-20150209 PPP tallylog Anaconda cron-2015021           5 PreLink test Audit cron-20150222 Restart Vboxadd-install.log bbbb.log             Dracut.log Samba Vboxadd-install-x11.log bbbb.log.1 GDM Secure VBoxGuestAdditions.log BitTorrent lastlog secure-20150202 vboxguestadditions-uninstall.log Boot.lo G Mail secure-20150209 wtmp boot.log-20150202 maillog secure-20150215 wtmp-20141 boot.log-20150209 maillog-20150202 secure-20150222 xferlog boot.log-20150215 maillog-20150209    Er xorg.0.log boot.log-20150222 maillog-20150215 spooler xorg.0.log.old btmp spooler-20150202 xorg.1.log btmp-20150302 messages spooler-20150209 xorg.9.log me ssages-20150309 Spooler-20150215 yum.log cron ntpstats spooler-20150222 cron-20150202 pm-powersave.log SSSD [roo T@localhost log]# ls Anaconda cron-20150215 prelink test Audit cron-20150222 R Estart vboxadd-install.log bbbb.log.1.1 Dracut.log Samba Vboxadd-install-x11.log BBB b.log.2 GDM Secure VBoxGuestAdditions.log BitTorrent Lastlog secure- 20150202 vboxguestadditions-uninstall.log boot.log Mail secure-20150209 wtmp boot.log-201502 Maillog secure-20150215 wtmp-20141120 boot.log-20150209 maillog-20150202 secure-20150222 xferlog b            oot.log-20150215 maillog-20150209 speech-dispatcher xorg.0.log boot.log-20150222 maillog-20150215           Xorg.0.log.old btmp maillog-20150222 spooler-20150202 xorg.1.log btmp-20150302 spooler-20150209 Xorg.9.log consolekit messages-20150309 spooler-20150215 yum.log cron ntpstats spooler-2015 0222 cron-20150202 pm-powersave.log SSSD cron-20150209 PPP tallylog [root@localhost log]# ls te St/aaaa.log bbbb.log bbbb.log.2 [root@localhost log]#


You can see that Aaaa.log and Bbbb.log have been deleted, but also saved two files bbbb.log.1.1 and bbbb.log.2.

And see/var/log/test under the file is Aaaa.log Bbbb.log bbbb.log.2 These three documents, this is why.

In the next one by one ways:

1 Write separate content in Bbbb.log and Bbbb.log.1, the following is the file content information:

[Root@localhost log]# cat Bbbb.log This
file was Bbbb.log
[root@localhost log]# cat Bbbb.log.1 This
file is BBB B.log.1
[Root@localhost log]#

2 following the execution of the logrotate command, the contents of bbbb.log.1.1 and bbbb.log.2:

[Root@localhost log]# cat bbbb.log.1.1 This
file was Bbbb.log
[root@localhost log]# cat bbbb.log.2 this
file I S bbbb.log.1
[root@localhost log]#

3) Next is the contents of Bbbb.log and bbbb.log.2 in the/var/log/test/directory:

[Root@localhost test]# cat Bbbb.log This
file are Bbbb.log
[root@localhost test]# cat bbbb.log.2 This
file is Bbbb.log.1
[Root@localhost test]#

Well, according to the contents of the document, you should probably know it now.

A, normally, after the command logrotate is executed:

Aaaa.log Wheel reserve for aaaa.log.1;

Bbbb.log Wheel reserve for bbbb.log.1;

But, when the logrotate command was executed, it was found that Bbbb.log wanted to dump the file Bbbb.log.1 TNN had already existed,

The system automatically bbbb.log.1 the original existing wheel into bbbb.log.2 (the renameing commands can also be seen during the logrotate run)

Well, I've already figured out why Bbbb.log.2 's content would be: This file is bbbb.log.1.

So why is the content of bbbb.log.1.1: This file is Bbbb.log.

Because when the content of the Prerotate ~script is run, the system bbbb.log the wheel to the Bbbb.log.1

The bbbb.log.1 wheel is then stored as a bbbb.log.1.1. So bbbb.log.1.1 's contents are: This file is Bbbb.log.

#######################

# Well, you think it's OK. #

# No,no,no. #

#######################

In fact, after the normal operation of Logrotate only file is a. 1 end of the will be deleted, but why there will be logrotate.log.1.1 it.

At the end of the logrotate there will always be such statements:

= = "Removing old log/var/log/aaaa.log.1

But after we execute LOGROTATE-VF test1, there are also statements that appear:

= = "Removing old log/var/log/aaaa.log.1
removing old Log/var/log/bbbb.log.1
Error:error opening/var/log/bbbb.log.1:no such file or directory

Here is an extra error, so the following execution of the removing old log/var/log/bbbb.log.1.1 is not executed;

Okay, now the question is, why is there an error?

According to my reasoning: When the aaaa.log.1 is deleted, the aaaa.log is open;

When the bbbb.log.1 is deleted, the bbbb.log is open;

When you delete bbbb.log.1.1, the bbbb.log.1 is open, but you have deleted the bbbb.log.1 in the previous step,

So logrotate does not go on, break the execution.

###################################################################################################

PS: The essence of any science is philosophy, it is difficult to understand this phrase.

###################################################################################################

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.