
Bashスクリプトで「foo.txt」として保存されるログファイルのサイズを制限する方法は?変数「LOGFILE = 50 mb」を入れたいので、そのサイズやLOGFILEのサイズに関係なく使用します。
これはDebian 7で完全に最新バージョンです。
答え1
ログ回転を使用する必要があります。これをする
猫/etc/logrotate.conf
/パス/foo.txt {
size 50M
create 700 root root
rotate 5
}
サイズ50M - logrotateは、ファイルサイズがこのサイズ以上である場合にのみ実行されます。
作成 - ソースファイルを回転させ、指定された権限、ユーザー、およびグループを使用して新しいファイルを作成します。
回転 – ログファイルの回転数を制限します。したがって、これは最後の5つのローテーションログファイルのみを保持します。
答え2
私は前tail
にこれをやったことがあります。これは迅速で汚れたソリューションに近いですが、logrotate
作業を完了します。
これが私がスクリプトに追加したものです。
# in the preceding lines, the log file is updated, with new content at the
# bottom.
# every update causes the file to exceed the size limit (set to 1MB)
# using tail, put the last 1MB of the file in a temporary file
/usr/bin/tail -c 1MB /your/path/here/logfile.log > /your/path/here//temp 2>&1
# overwrite the older and oversized log file with the new one
/bin/mv /your/path/here/temp /your/path/here/logfile.log
答え3
そんなこと?
LF=foo.txt
typeset -i LFSB LFSM LOGFILE=50
let LFSB=$(stat -c "%s" $LF)
# This is bytes - turn into MB, base 2
let LFSM=${LFSB}/1048576
if [ $LFSM -gt $LOGFILE ]
then
echo Logfile $LF is greater than $LOGFILE MB
else
echo Logfile $LF is less or equal than $LOGFILE MB
fi