问题描述
网络上成千上万的人使用 tail 来监视日志文件中的字符串,如果找不到,则继续监视。这与我遇到的问题相反——我试图从根本上节省资源,我不想要一个持续的线程来监视日志:我试图设置一个 cron 作业来每半小时运行一次。但这意味着 Bash 脚本,tail 和 grep,必须监视字符串然后退出,无论是否找到它。这是我最好的尝试:
LOG_FILE="/var/log/syslog"
SEARCHED="fallback errored"
( tail -f -n0 "$LOG_FILE" & ) | while IFS= read -r line; do
if [ $(echo "${line}" | grep -q "${SEARCHED}") ] ; then
curl -X POST https://maker.ifttt.com/trigger/EthFail/with/key/XXXXX
touch "MonitorRanAndIamProofWhen"
fi
done
exit 0
但它一直在运行...... 尝试将 tail 放在其自己的子进程中,并在 read 语句中添加 -t 时间标志。仍然坐在那里看。
解决方法
如果要查找字符串 ${SEARCHED}
,则不需要测试命令 [ ]
。
( tail -f -n0 "$LOG_FILE" & ) | while IFS= read -r line; do
if $(echo "${line}" | grep -q "${SEARCHED}") ; then
那么,下面的命令是否意味着如果创建了以下文件,子进程就结束了?
touch "MonitorRanAndIamProofWhen"