以下脚本需要检查目录是否存在且大小是否超过 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