Unix为了等待特定的字符串或超时后退出解决方案 [英] Unix to tail solution for waiting for a specific string or quitting after a timeout

查看:88
本文介绍了Unix为了等待特定的字符串或超时后退出解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改进一个服务(守护进程)脚本来显示tomcat输出(当从tty运行时)直到应用程序被有效地启动。

Tomcat应用程序可以最多需要10分钟才能开始,这对于看到会发生什么非常有用。

但是,我们需要确保此启动脚本不会无限期地运行,如果启动确认消息未能出现在日志中。



我知道互联网充满了类似的问题,但到目前为止我无法找到一个明确的解决方案,特别是一个工作,不需要默认情况下不可用的工具。



要求:


  1. 运行时不需要在最近的Debian,Ubuntu,RedHat和OS X上安装任何东西

  2. 结束时不会留下任何进程(由于任何原因)

  3. 显示记录行到标准输出

  4. 日志文件是只读的,您不能触摸它

  5. 日志文件可以是r如果您可以使其工作在同一行中,则可以获得额外的荣誉

到目前为止发现的问题:


  • 读-t code>非法选项-t



到目前为止的部分解决方案参考:



$ b $如果你想让一个脚本监视等待特定字符串的tomcat输出,下面的解决方案应该可以工作,并且它不会使用读-t

 (tail -f server.log& ((tail -f server.log | grep -l STARTED&& kill -9`pstree -pa $$ | grep -E'\-(tail | sleep),'| sed's / ^ [^ 0 -9] * \([0-9] * \)。* / \ 1 /'| grep -vw $$ | xargs`)& sleep 900&& kill -9`pstree -pa $ $ | grep -E'\-(tail | sleep),'| sed's / ^ [^ 0-9] * \([0-9] * \)。* / \ 1 /'| grep -vw $$ | xargs`&&& exit 3)); [$? -eq 3]&& echo'服务器在5分钟后不能运行'|| echo'server up'

它正在搜索字符串 STARTED 在日志文件 server.log 内部,超时时间为15分钟(900秒)。
我不确定你在Debian中有什么可用的。你应该有 pstree ,其输出应该类似于:

  $ pstree -pa 1803 
bash,1803
└─bash,7850
├─bash,7852
│├─bash,7853
││├─grep,7856 --color = auto -l STARTED
││└─tail,7855 -f server.log
│└─睡眠,7854 20
└─tail,7851 -f server.log

剩下的我相信你应该有: kill sed sleep , grep echo xargs



该行包含一条命令和命令结果的最终评估,以便在服务器启动时打印。该命令分为3个部分:


  1. 根据要求输出日志内容。

  2. 监控输出寻找特定字符串 STARTED ;如果找到 kill 第1部分和第3部分。
  3. 等待15分钟,如果15分钟通过,则 kill 第1部分和第2部分,
    用代码3退出,向外部评估表明
    整个命令由于超时而结束。

评论:

  (tail -f server.log&#part 1  - 只输出日志内容
(#part 2
(tail -f server.log | grep -l STARTED#搜索字符串
&& kill -9`pstree -pa $$ | grep -E'\-(tail | sleep),'|
sed's / ^ [^ 0-9] * \([ 0-9] * \)。* / \ 1 /'| grep -vw $$ |
xargs`#如果找到了,就杀掉剩下的尾巴并睡觉
)&#如果命令第2部分结束,退出代码不是3
睡眠900#第3部分:睡眠15分钟
&& kill -9`pstree -pa $$ | grep -E'\-(tail | | sleep},'|
sed's / ^ [^ 0-9] * \([0-9] * \)。* / \ 1 /'| grep -vw $$ |
xargs`#如果时间过去了,杀死剩下的尾巴并睡觉
&& exit 3#退出代码3,以便外部评估
#知道它以超时结束

); #命令结束;每当它完成时,执行下面的评估,
#检查退出代码以打印适当的消息
[$? -eq 3]&& echo'服务器在5分钟后不能运行'|| echo'server up'

请注意,由于 bash 管道工作,如果找到 STARTED 字符串,该命令实际上只会在添加额外行后退出并打印 server up 消息 STARTED 后的 server.log 后面。这是因为当打印 STARTED 行时, grep 会找到它,打印它的输出并退出,关闭它的输入。但是,相应的 tail 将继续运行,直到它还有其他内容可以写入;只有 tail 才会意识到它的输出已关闭,然后死亡,继续执行 kill 命令,这将导致到评估。 AFAIK没有解决方法。您应该确保在 STARTED 消息之后将日志打印到日志中(如果尚未打印)。


I am trying to improve a service (daemon) script to display tomcat output (when run from a tty) until the application is effectively started.

Tomcat apps can take up to 10 minutes to start and it's extremely useful to see what happens.

Still, we need to be sure that this startup script will not run indefinitely if the startup confirmation message is failing to appear in the logs.

I know that the internet is full of similar questions, but so far I wasn't able to identify a clear solution, especially one that works without requiring utilities that are not available by default.

Requirements:

  1. run without requiring you to install anything on recent Debian, Ubuntu, RedHat and OS X
  2. no processes left behind when it ends (due to any reason)
  3. display the logged lines to stdout
  4. log file is readonly, you cannot touch it
  5. log file can be rotated without breaking the tool
  6. extra kudos if you can make it work in one line

Identified problems so far:

  • read -t not available on current Debian, Illegal option -t

References to partial solutions so far:

解决方案

If you want a script to monitor the tomcat output waiting for a specific string, the solution below should work, and it doesn't use read -t.

( tail -f server.log & ( (tail -f server.log | grep -l STARTED && kill -9 `pstree -pa $$ | grep -E '\-(tail|sleep),' | sed 's/^[^0-9]*\([0-9]*\).*/\1/' | grep -vw $$ | xargs` ) & sleep 900 && kill -9 `pstree -pa $$ | grep -E '\-(tail|sleep),' | sed 's/^[^0-9]*\([0-9]*\).*/\1/' | grep -vw $$ | xargs` && exit 3 ) ) ; [ $? -eq 3 ]  && echo 'server not up after 5 min'  || echo 'server up'

It is searching for the string "STARTED" inside the log file server.log, with a timeout of 15 minutes (900 seconds). I'm not sure what you have available in your Debian. You should have pstree, whose output should be similar to:

$ pstree -pa 1803
bash,1803
  └─bash,7850
      ├─bash,7852
      │   ├─bash,7853
      │   │   ├─grep,7856 --color=auto -l STARTED
      │   │   └─tail,7855 -f server.log
      │   └─sleep,7854 20
      └─tail,7851 -f server.log

The rest I'm sure you should have: kill, sed, sleep, grep, echo, xargs.

Some explanation of what is happening. This line contains one command and the final evaluation of the command results, to print if the server is up or not. The command is divided in 3 parts:

  1. simply output the log content, as requested.
  2. monitor the output looking for the specific string "STARTED"; if found, kill parts 1 and 3.
  3. wait for 15 minutes, and if 15 minutes pass, kill parts 1 and 2, exiting with code 3 to signal to the outside evaluation that the whole command ended due to timeout.

With comments:

( tail -f server.log & # part 1 - just output the log content
  ( # part 2
    (tail -f server.log | grep -l STARTED # search for the string
     && kill -9 `pstree -pa $$ | grep -E '\-(tail|sleep),' | 
        sed 's/^[^0-9]*\([0-9]*\).*/\1/' | grep -vw $$ | 
        xargs` # if found, kill the remaining tails and sleeps
    ) & # if the command ends in part 2, exit code is not 3
  sleep 900 # part 3: sleep 15 minutes
  && kill -9 `pstree -pa $$ | grep -E '\-(tail|sleep),' | 
              sed 's/^[^0-9]*\([0-9]*\).*/\1/' | grep -vw $$ | 
              xargs` # if time passed, kill the remaining tails and sleeps
  && exit 3 # exit with code 3, so that the outside evaluation 
            # knows it ended by timeout
  )
) ; # end of command; whenever it finishes, the evaluation below is executed, 
    # checking the exit code to print the appropriate message
[ $? -eq 3 ]  && echo 'server not up after 5 min'  || echo 'server up'

Note that, due to how bash pipes work, if the "STARTED" string is found the command will actually only exit and print the message "server up" after an extra line is added to server.log after the line with "STARTED". That's because when the line "STARTED" is printed, grep will find it, print its output and exit, closing its input. However, the corresponding tail will continue to run until it has something else to write; only then tail will realize its output is closed, and then will die, moving on to the kill command that will lead to the evaluation. There's no workaround to that, AFAIK. You should make sure something is printed to the log after the "STARTED" message, if it's not already.

这篇关于Unix为了等待特定的字符串或超时后退出解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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