如果日志文件中的字符串(找到相关字符串),我需要编写一个脚本来创建和警告并获取线程转储 - /tmp/area.log
. 到目前为止,我可以在 2 个单独的脚本中执行此操作,但希望将它们合并为一个。
脚本 1:创建警报
filelocation=$1
string=$2
count=$(cat $1 | grep -i "$2" | wc -l)
if [[ "$count" -ge 1 ]];
then
echo "WARNING: There are $count occurrences of $2 in log file"
exit 1
else
echo "OK: No lines with $2 in log file"
exit 0
fi
脚本 2:创建线程转储
#!/bin/bash
PID=$(ps -ef | grep java | awk '{print $2}')
N=3
INTERVAL=5
for ((i=1;i<=$N;i++))
do
# d=$(date +%Y%m%d-%H:%M:%S)
# dump="/tmp/Threaddump-$PID-$d.txt"
dump="/tmp/ThreadDump-`hostname`-`date '+%F-%H:%M:%S'`.gz"
echo $i of $N: $dump
/opt/jdk1.8.0_121/jdk1.7.0_40/bin/jstack -l $PID > $dump
sleep $INTERVAL
done