Bash : 如何在连续循环中拖尾最新文件
Bash : How to tail latest file in continuous loop
我正在尝试编写脚本来监视日志文件,并在出现特定错误时执行特定功能。
我的主要代码可以正常工作,但遇到一个问题。
应用程序在系统日期更改时创建新的日志文件(日志文件名称模式为 LOG.NODE1.DDMMYYYY),如何让我的代码自动切换到创建的新文件。到目前为止,以下是我的脚本,
#!/bin/sh
logfile=$(ls -t $DIR"/env/log/LOG"* | head -n 1)
echo $logfile
tail -f $logfile | while read LOGLINE
do
if [[ "${LOGLINE}" == *";A database exception has occurred: FATAL DBERR: SQL_ERROR: ORA-00001: unique constraint (IX_TEST1) violated"* ]];
then
#Do something
fi
done
#!/bin/bash
# ^^^^ -- **NOT** /bin/sh
substring=";A database exception has occurred: FATAL DBERR: SQL_ERROR: ORA-00001: unique constraint (IX_TEST1) violated"
newest=
timeout=10 # number of seconds of no input after which to look for a newer file
# sets a global shell variable called "newest" when run
# to keep overhead down, this avoids invoking any external commands
find_newest() {
set -- "${DIR?The variable DIR is required}"/env/log/LOG*
[[ -e || -L ]] || return 1
newest=; shift
while (( $# )); do
[[ -nt $newest ]] && newest=
shift
done
}
while :; do
find_newest # check for newer files
# if the newest file isn't the one we're already following...
if [[ $tailing_from_file != "$newest" ]]; then
exec < <(tail -f -- "$newest") # start a new copy of tail following the newer one
tailing_from_file=$newest # and record that file's name
fi
if read -t "$timeout" -r line && [[ $line = *"$substring"* ]]; then
echo "Do something here"
fi
done
我正在尝试编写脚本来监视日志文件,并在出现特定错误时执行特定功能。 我的主要代码可以正常工作,但遇到一个问题。 应用程序在系统日期更改时创建新的日志文件(日志文件名称模式为 LOG.NODE1.DDMMYYYY),如何让我的代码自动切换到创建的新文件。到目前为止,以下是我的脚本,
#!/bin/sh
logfile=$(ls -t $DIR"/env/log/LOG"* | head -n 1)
echo $logfile
tail -f $logfile | while read LOGLINE
do
if [[ "${LOGLINE}" == *";A database exception has occurred: FATAL DBERR: SQL_ERROR: ORA-00001: unique constraint (IX_TEST1) violated"* ]];
then
#Do something
fi
done
#!/bin/bash
# ^^^^ -- **NOT** /bin/sh
substring=";A database exception has occurred: FATAL DBERR: SQL_ERROR: ORA-00001: unique constraint (IX_TEST1) violated"
newest=
timeout=10 # number of seconds of no input after which to look for a newer file
# sets a global shell variable called "newest" when run
# to keep overhead down, this avoids invoking any external commands
find_newest() {
set -- "${DIR?The variable DIR is required}"/env/log/LOG*
[[ -e || -L ]] || return 1
newest=; shift
while (( $# )); do
[[ -nt $newest ]] && newest=
shift
done
}
while :; do
find_newest # check for newer files
# if the newest file isn't the one we're already following...
if [[ $tailing_from_file != "$newest" ]]; then
exec < <(tail -f -- "$newest") # start a new copy of tail following the newer one
tailing_from_file=$newest # and record that file's name
fi
if read -t "$timeout" -r line && [[ $line = *"$substring"* ]]; then
echo "Do something here"
fi
done