2012年4月24日 星期二

利用newsyslog製作log循環備份

newsyslog 是安裝 freebsd 後就會有的套件,其主要功用是配合crontab排程,將log製作備份。

參考:
http://www.weithenn.org/cgi-bin/wiki.pl?newsyslog.conf
http://contest.ks.edu.tw/syshtml/freebsd-newsyslog.html

補充:
 
     ※要是我們要新加入的一些非預設的,程式(例如www)所產生的紀錄檔怎麼辦?
         /var/log/httpd-access.log         640    5    *    @T00     Z /var/run/httpd.pid 30
         /var/log/httpd-error.log          640    5    *    @T00     Z /var/run/httpd.pid 30
 
         第一欄是紀錄檔的位置, 第二欄是紀錄檔的權限 (mode),5 表示在硬碟
         內保留過去六次處理的紀錄(log.0.gz~ log.5.gz), 比六次還舊的紀錄
         就把他砍了。* 表示不管紀錄檔的大小。 @T00 表示每天零點零分的時候
         處理這個檔案,Z 表示把處理過的檔案壓縮起來, 節省空間。
         
   
     ※為什麼後面要加上/var/run/httpd.pid 30?
         因為 newsyslog 在系統 default 的 contable (/etc/crontab) 裡被預先
         設定為每一個小時啟動一次,然而在 newsyslog.conf 裡設定讓 apache log
         自動 rotate,不過發現 newsyslog 後接下來的 apache log 都不會寫入了,
         只留下一行 newsyslog[66253]: logfile turned over
         這個問題出在當 newsyslog rotate log之後,沒有送個 SIGHUP 給 apache,
         導致 apache的log 寫到不知道什麼地方去,所以要在 newsyslog.conf 裡面
         有關 httpd-access.log 及 httpd-error.log 的部份後面加上要 KILL -HUP
         的 pid 檔,才能正常運作。 
 
==== 20120425 ====

為什麼httpd.pid的sig_num 要設定為30? 不能設定其他值嗎?


 
hwakeye  在下面有作解釋
===== 
 
 
flags   This optional field is made up of one or more characters that
             specify any special processing to be done for the log files
             matched by this line.  The following are valid flags:

             B       indicates that the log file is a binary file, or has some
                     special format.  Usually newsyslog(8) inserts an ASCII
                     message into a log file during rotation.  This message is
                     used to indicate when, and sometimes why the log file was
                     rotated.  If B is specified, then that informational mes-
                     sage will not be inserted into the log file.

             C       indicates that the log file should be created if it does
                     not already exist, and if the -C option was also speci-
                     fied on the command line.

             D       indicates that newsyslog(8) should set the UF_NODUMP flag
                     when creating a new version of this log file.  This
                     option would affect how the dump(8) command treats the
                     log file when making a file system backup.

             G       indicates that the specified logfile_name is a shell pat-
                     tern, and that newsyslog(8) should archive all filenames
                     matching that pattern using the other options on this
                     line.  See glob(3) for details on syntax and matching
                     rules.

             J       indicates that newsyslog(8) should attempt to save disk
                     space by compressing the rotated log file using bzip2(1).

             N       indicates that there is no process which needs to be sig-
                     naled when this log file is rotated.

             U       indicates that the file specified by path_to_pid_file
                     will contain the ID for a process group instead of a
                     process.  This option also requires that the first line
                     in that file be a negative value to distinguish it from a
                     process ID.

             Z       indicates that newsyslog(8) should attempt to save disk
                     space by compressing the rotated log file using gzip(1).

             -       a minus sign will not cause any special processing, but
                     it can be used as a placeholder to create a flags field
                     when you need to specify any of the following fields.

2 則留言:

  1. 剛剛很好奇Signal 30這東西就幫你查了一下
    Signal 30是USR1,也就是User-defined signal 1
    在Apache裡被定義為Graceful Restart
    http://httpd.apache.org/docs/2.2/stopping.html#graceful
    所以用意應該就是要避免使用預設的HUP造成process被不正常的強制關閉吧

    回覆刪除