Help us understand the problem. What is going on with this article?

FreeBSD の newsyslog で楽々ログローテーション

More than 1 year has passed since last update.

FreeBSDではデフォルトでnewsyslogというプログラムがログのローテーションを行なっています
/etc/crontabに下記のような記述があるので1時間に1回動作しているようです

0   *   *   *   *   root    newsyslog

いつからか忘れましたが/etc/newsyslog.confで下記のようにファイルが
includeされるようになったのでどちらかのディレクトリにファイルを置くだけで設定が追加できます

<include> /etc/newsyslog.conf.d/*
<include> /usr/local/etc/newsyslog.conf.d/*

※少なくともFreeBSD11.2ではinclude対応済

FreeBSDではNginxやApacheだけをインストールしてもログローテーションされないので
/etc/newsyslog.conf.d/nginx_apache を追加してみましょう

# logfilename          [owner:group]    mode count size when  flags [/pid_file] [sig_num]
/var/log/nginx/*.log   root:wheel       644  7     *    $D05  BGX   /var/run/nginx.pid 30
/var/log/httpd/*.log   root:wheel       644  7     *    $D05  BGX   /var/run/httpd.pid 1

count=7でバックアップファイルは7個まで
when の $D05 で毎日5時に実行
flags の BGX の意味は下記の通り

  • B: ローテート時にログファイルにメッセージを残さない
  • G: logfilenameで指定したパスがシェルパターンであることを示す(*を使っているため)
  • X: ログファイルの圧縮にxzを使用

sig_numはログローテート時にプロセスに投げるシグナル番号を指定30はSIGUSR1、1はSIGHUP
上記のファイルを設置すると数日後下記のようになります

$ ls -l /var/log/nginx/access.log* /var/log/nginx/error.log* /var/log/httpd/access.log* /var/log/httpd/error.log*
-rw-r--r--  1 root    wheel   6958 Dec  4 15:06 /var/log/httpd/access.log
-rw-r--r--  1 root    wheel   3336 Dec  4 05:00 /var/log/httpd/access.log.0.xz
-rw-r--r--  1 root    wheel   5452 Dec  3 05:00 /var/log/httpd/access.log.1.xz
-rw-r--r--  1 root    wheel   5116 Dec  2 05:00 /var/log/httpd/access.log.2.xz
-rw-r--r--  1 root    wheel   1564 Dec  1 05:00 /var/log/httpd/access.log.3.xz
-rw-r--r--  1 root    wheel   6328 Nov 30 05:00 /var/log/httpd/access.log.4.xz
-rw-r--r--  1 root    wheel   7432 Nov 29 05:00 /var/log/httpd/access.log.5.xz
-rw-r--r--  1 root    wheel   3624 Nov 28 05:00 /var/log/httpd/access.log.6.xz
-rw-r--r--  1 root    wheel    706 Dec  4 11:39 /var/log/httpd/error.log
-rw-r--r--  1 root    wheel   1744 Dec  4 05:00 /var/log/httpd/error.log.0.xz
-rw-r--r--  1 root    wheel   4156 Dec  3 05:00 /var/log/httpd/error.log.1.xz
-rw-r--r--  1 root    wheel   3180 Dec  2 05:00 /var/log/httpd/error.log.2.xz
-rw-r--r--  1 root    wheel    352 Dec  1 05:00 /var/log/httpd/error.log.3.xz
-rw-r--r--  1 root    wheel   4392 Nov 30 05:00 /var/log/httpd/error.log.4.xz
-rw-r--r--  1 root    wheel   4688 Nov 29 05:00 /var/log/httpd/error.log.5.xz
-rw-r--r--  1 root    wheel   2148 Nov 28 05:00 /var/log/httpd/error.log.6.xz
-rw-r--r--  1 nobody  wheel  84079 Dec  4 15:05 /var/log/nginx/access.log
-rw-r--r--  1 root    wheel   5580 Dec  4 05:00 /var/log/nginx/access.log.0.xz
-rw-r--r--  1 root    wheel   4140 Dec  3 05:00 /var/log/nginx/access.log.1.xz
-rw-r--r--  1 root    wheel   4500 Dec  2 05:00 /var/log/nginx/access.log.2.xz
-rw-r--r--  1 root    wheel   7664 Dec  1 05:00 /var/log/nginx/access.log.3.xz
-rw-r--r--  1 root    wheel   3812 Nov 30 05:00 /var/log/nginx/access.log.4.xz
-rw-r--r--  1 root    wheel   1172 Nov 29 05:00 /var/log/nginx/access.log.5.xz
-rw-r--r--  1 root    wheel   7436 Nov 28 05:00 /var/log/nginx/access.log.6.xz
-rw-r--r--  1 nobody  wheel  74138 Dec  4 13:48 /var/log/nginx/error.log
-rw-r--r--  1 root    wheel   6104 Dec  4 05:00 /var/log/nginx/error.log.0.xz
-rw-r--r--  1 root    wheel   3484 Dec  3 05:00 /var/log/nginx/error.log.1.xz
-rw-r--r--  1 root    wheel   4032 Dec  2 05:00 /var/log/nginx/error.log.2.xz
-rw-r--r--  1 root    wheel   8224 Dec  1 05:00 /var/log/nginx/error.log.3.xz
-rw-r--r--  1 root    wheel   3768 Nov 30 05:00 /var/log/nginx/error.log.4.xz
-rw-r--r--  1 root    wheel    416 Nov 29 05:00 /var/log/nginx/error.log.5.xz
-rw-r--r--  1 root    wheel   7208 Nov 28 05:00 /var/log/nginx/error.log.6.xz
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away