Shell 函数在特定时间跟踪特定字符串的日志文件 [英] Shell function to tail a log file for a specific string for a specific time

查看:10
本文介绍了Shell 函数在特定时间跟踪特定字符串的日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做以下事情来确保我的应用服务器是

I need to the following things to make sure my application server is

  1. 为特定字符串拖尾日志文件
  2. 在该字符串被打印之前保持阻塞状态
  3. 但是,如果字符串在大约 20 分钟内未打印,则退出并抛出异常消息,例如服务器启动时间超过 20 分钟"
  4. 如果日志文件中打印了字符串,则退出循环并继续.

有没有办法在 while 循环中包含超时?

Is there a way to include time outs in a while loop ?

推荐答案

#!/bin/bash
tail -f logfile | grep 'certain_word' | read -t 1200 dummy_var
[ $? -eq 0 ]  && echo 'ok'  || echo 'server not up'

这会读取写入日志文件的任何内容,搜索某个字,如果一切正常,则回显 ok,否则等待 1200 秒(20 分钟)后,它会抱怨.

This reads anything written to logfile, searches for certain_word, echos ok if all is good, otherwise after waiting 1200 seconds (20 minutes) it complains.

这篇关于Shell 函数在特定时间跟踪特定字符串的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆