
以下腳本需要檢查目錄是否存在且大小是否超過 10000KB,歸檔並刪除檔案。
當使用 crontab 運行腳本並收到錯誤代碼 1 時,output.log 表示 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