
次のスクリプトは、ディレクトリが存在し、サイズが 10000 KB を超えているかどうかを確認し、ファイルをアーカイブして削除します。
output.log には、crontab を使用してスクリプトを実行し、エラー コード 1 を取得したときに tracedir が存在しないと表示されますが、実際には存在します。
また、for ループでディレクトリを印刷しようとしましたが、tracedir 値は適切に印刷されました。
#!/bin/bash
sts=$(date)
echo "Script started running at $sts" > output.log
lsnrdir=$ORACLE_BASE/diag/tnslsnr/$HOSTNAME/listener/alert/
thr=10000
for i in $(ps aux | grep ora_smon | grep -v grep | awk '{print $11}' | cut -c10-17)
do
a=$(echo "$i" | tr '[:upper:]' '[:lower:]')
tracedir=$ORACLE_BASE/diag/rdbms/$a/$i/trace/
if [ -d $tracedir ]; then
size="$(du -s --exclude=*.tar.gz $tracedir | awk '{print $1}')"
if [ "$size" -ge $thr ]; then
echo "The trace and alert log size in $i is: $size KB" >> output.log
echo "Trace and alert log archive started in $i ...." >> output.log
tar -czf $tracedir/dbtrace.tar.gz $tracedir
status=$?
if [ $status -ne 0 ]; then
echo "The error code is---"$status >> output.log
fi
echo "Trace and alert log archive completed in $i" >> output.log
sleep 5
tail -n 100 $tracedir/alert_$i.log > $tracedir/$i.log
mv $tracedir/$i.log $tracedir/alert_$i.log
find $tracedir -name "*.trc" -delete
find $tracedir -name "*.trm" -delete
echo "Removed trace files and log files in $i" >> output.log
else
echo "Trace log size in $i is not greater than $thr KB" >> output.log
fi
else
echo "No trace directory exists in $i" >> output.log
fi
done