LoginSignup
1
1

More than 5 years have passed since last update.

【Linux】 logrotateコマンド使わずにログローテートをしてみる

Posted at

サーバなどでログのローテーションする時は、logrotateコマンド使うのが一番簡単な手段。
もし、logrotateコマンド使えなかったらどうしようということで、ちょっとスクリプト作ってみました。

今回は、下記のような条件を想定してスクリプトを作ってみました。

-

0.条件

  • logrotateコマンドを使用しない
  • スクリプトはcronで1時間毎に実行されることを想定
  • 日次のログローテーションは0時台に実行する
  • 過去ログは20世代保持とする

なお、検証環境はAWSでインスタンスを作成しました。

[root@testhost ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.2 (Maipo)
[root@testhost ~]# 

-

1.スクリプト

※今回は、想定通りに動作するかが確認できればいいのでスクリプト自体も簡単になっています。。

#!/bin/bash

# Variable definition
FILE="sample.txt"
DIR="/tmp/test/"
DATA=${DIR}${FILE}
FLG=`find ${DIR} -mtime +20 | grep -c ".txt"`
HTM=`date +'%k'`
CHK=14
LOG_CNT=20

# Daily rotation
if [ -e ${DATA} ]; then
    if [ ${HTM} -eq ${CHK} ]; then
        cp -p ${DATA} ${DATA}_`date +'%Y%m%d'`
        cat /dev/null > ${DATA}
        echo "Complete! Daily rotation."
    fi
fi

# Generation management
if [ ${FLG} -ne 0 ]; then
    find ${DIR} -mtime +${LOG_CNT} -exec rm -f {} \;
    echo "Complete! Generation management."
fi

exit 0

-

2.実機確認

2.1.事前状態

[root@testhost test]# ll
total 16
-rw-r--r--. 1 root root 325 May  7 23:54 INFILE
-rwxrwxrwx. 1 root root 506 May  7 23:55 logtotate.sh
-rwxrwxrwx. 1 root root 330 May  7 23:54 make_testfile.sh
-rw-r--r--. 1 root root   0 Apr  1 12:00 sample.txt_20160401
-rw-r--r--. 1 root root   0 Apr  2 12:00 sample.txt_20160402
-rw-r--r--. 1 root root   0 Apr  3 12:00 sample.txt_20160403
-rw-r--r--. 1 root root   0 Apr  4 12:00 sample.txt_20160404
-rw-r--r--. 1 root root   0 Apr  5 12:00 sample.txt_20160405
-rw-r--r--. 1 root root   0 Apr  6 12:00 sample.txt_20160406
-rw-r--r--. 1 root root   0 Apr  7 12:00 sample.txt_20160407
-rw-r--r--. 1 root root   0 Apr  8 12:00 sample.txt_20160408
-rw-r--r--. 1 root root   0 Apr  9 12:00 sample.txt_20160409
-rw-r--r--. 1 root root   0 Apr 10 12:00 sample.txt_20160410
-rw-r--r--. 1 root root   0 Apr 11 12:00 sample.txt_20160411
-rw-r--r--. 1 root root   0 Apr 12 12:00 sample.txt_20160412
-rw-r--r--. 1 root root   0 Apr 13 12:00 sample.txt_20160413
-rw-r--r--. 1 root root   0 Apr 14 12:00 sample.txt_20160414
-rw-r--r--. 1 root root   0 Apr 15 12:00 sample.txt_20160415
-rw-r--r--. 1 root root   0 Apr 16 12:00 sample.txt_20160416
-rw-r--r--. 1 root root   0 Apr 17 12:00 sample.txt_20160417
-rw-r--r--. 1 root root   0 Apr 18 12:00 sample.txt_20160418
-rw-r--r--. 1 root root   0 Apr 19 12:00 sample.txt_20160419
-rw-r--r--. 1 root root   0 Apr 20 12:00 sample.txt_20160420
-rw-r--r--. 1 root root   0 Apr 21 12:00 sample.txt_20160421
-rw-r--r--. 1 root root   0 Apr 22 12:00 sample.txt_20160422
-rw-r--r--. 1 root root   0 Apr 23 12:00 sample.txt_20160423
-rw-r--r--. 1 root root   0 Apr 24 12:00 sample.txt_20160424
-rw-r--r--. 1 root root   0 Apr 25 12:00 sample.txt_20160425
-rw-r--r--. 1 root root   0 Apr 26 12:00 sample.txt_20160426
-rw-r--r--. 1 root root   0 Apr 27 12:00 sample.txt_20160427
-rw-r--r--. 1 root root   0 Apr 28 12:00 sample.txt_20160428
-rw-r--r--. 1 root root   0 Apr 29 12:00 sample.txt_20160429
-rw-r--r--. 1 root root   0 Apr 30 12:00 sample.txt_20160430
-rw-r--r--. 1 root root   0 May  1 12:00 sample.txt_20160501
-rw-r--r--. 1 root root   0 May  2 12:00 sample.txt_20160502
-rw-r--r--. 1 root root   0 May  3 12:00 sample.txt_20160503
-rw-r--r--. 1 root root   0 May  4 12:00 sample.txt_20160504
-rw-r--r--. 1 root root   0 May  5 12:00 sample.txt_20160505
-rw-r--r--. 1 root root   0 May  6 12:00 sample.txt_20160506
-rw-r--r--. 1 root root   0 May  7 12:00 sample.txt_20160507
-rw-r--r--. 1 root root 871 May  7 23:51 sample.txt
[root@testhost test]# 

2.2.スクリプト実行〜実行後確認

[root@testhost test]# date;./logtotate.sh;ll;date
Sun May  8 00:00:36 EDT 2016
Complete! Daily rotation.
Complete! Generation management.
total 16
-rw-r--r--. 1 root root 325 May  7 23:54 INFILE
-rwxrwxrwx. 1 root root 506 May  7 23:55 logtotate.sh
-rwxrwxrwx. 1 root root 330 May  7 23:54 make_testfile.sh
-rw-r--r--. 1 root root   0 Apr 17 12:00 sample.txt_20160417
-rw-r--r--. 1 root root   0 Apr 18 12:00 sample.txt_20160418
-rw-r--r--. 1 root root   0 Apr 19 12:00 sample.txt_20160419
-rw-r--r--. 1 root root   0 Apr 20 12:00 sample.txt_20160420
-rw-r--r--. 1 root root   0 Apr 21 12:00 sample.txt_20160421
-rw-r--r--. 1 root root   0 Apr 22 12:00 sample.txt_20160422
-rw-r--r--. 1 root root   0 Apr 23 12:00 sample.txt_20160423
-rw-r--r--. 1 root root   0 Apr 24 12:00 sample.txt_20160424
-rw-r--r--. 1 root root   0 Apr 25 12:00 sample.txt_20160425
-rw-r--r--. 1 root root   0 Apr 26 12:00 sample.txt_20160426
-rw-r--r--. 1 root root   0 Apr 27 12:00 sample.txt_20160427
-rw-r--r--. 1 root root   0 Apr 28 12:00 sample.txt_20160428
-rw-r--r--. 1 root root   0 Apr 29 12:00 sample.txt_20160429
-rw-r--r--. 1 root root   0 Apr 30 12:00 sample.txt_20160430
-rw-r--r--. 1 root root   0 May  1 12:00 sample.txt_20160501
-rw-r--r--. 1 root root   0 May  2 12:00 sample.txt_20160502
-rw-r--r--. 1 root root   0 May  3 12:00 sample.txt_20160503
-rw-r--r--. 1 root root   0 May  4 12:00 sample.txt_20160504
-rw-r--r--. 1 root root   0 May  5 12:00 sample.txt_20160505
-rw-r--r--. 1 root root   0 May  6 12:00 sample.txt_20160506
-rw-r--r--. 1 root root   0 May  7 12:00 sample.txt_20160507
-rw-r--r--. 1 root root 871 May  7 23:51 sample.txt_20160508
-rw-r--r--. 1 root root   0 May  8 00:00 sample.txt
Sun May  8 00:00:36 EDT 2016
[root@testhost test]#
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1