自动停止“tail -f catalina”。当出现异常时,或服务器正在运行。

时间:2021-04-23 16:16:09

I have set up an automated deployment script (in shell script) for my web application.

我已经为我的web应用程序设置了一个自动部署脚本(在shell脚本中)。

It uses java, tomcat, maven and a postgres database.

它使用java、tomcat、maven和postgres数据库。

The deployment script does this:

部署脚本执行以下操作:

  1. builds the deployable application from source repository
  2. 从源存储库构建可部署的应用程序。
  3. stops tomcat
  4. 停止tomcat
  5. applies database migration patches
  6. 数据库迁移应用补丁
  7. deploys the war files in tomcat
  8. 在tomcat中部署war文件。
  9. starts tomcat (by invoking $TOMCAT_HOME/bin/startup.sh)
  10. 启动tomcat(通过调用$TOMCAT_HOME/bin/startup.sh)
  11. exits with a success message
  12. 带有成功消息的出口。

It's all working and it's pretty neat - but it needs a little improvement. You see, even though it exits with a success message, sometimes the deploy was not successful because the web application did not start correctly.

一切都很有效,而且非常整洁,但需要一点改进。您可以看到,即使它以成功消息的形式退出,有时部署也不成功,因为web应用程序没有正确启动。

I would like to refactor steps 5 and 6 so that after bring up the tomcat server, the deployment script would "tail -f" in the catalina.out file, looking either for a "server started successfully" message or an exception stack trace.

我想重构步骤5和步骤6,以便在打开tomcat服务器之后,部署脚本将在catalina中“tail -f”。输出文件,查找“服务器启动成功”消息或异常堆栈跟踪。

The tail -f output up to that point should be part of the output of the deployment script, and step 6 would "exit 0" or "exit 1" accordingly.

尾部-f输出到那个点应该是部署脚本输出的一部分,步骤6将“退出0”或“退出1”。

I know that should be possible, if not in shell script, maybe with python. The problem is I'm a java specialist - and by specialist I mean I suck at everything else :-)

我知道这应该是可能的,如果不是在shell脚本中,可能是使用python。问题是,我是一个java专家——我的专业是,我讨厌其他任何东西:-)

Help please? :-)

帮助吗?:-)

3 个解决方案

#1


0  

Maybe something like this?

也许是这样的?

tmp=$(mktemp -t catalina.XXXXXXX) || exit 136
trap 'rm "$tmp"' 0
trap 'exit 255' 2 15
tail -n 200 catalina.out >"$tmp"
if grep -q error "$tmp"; then
    cat "$tmp"
    exit 1
fi
exit 0

On the other hand, if startup.sh were competently coded, you could just

另一方面,如果创业。sh是很好的编码,你可以。

if startup.sh; then
    tail -f catalina.out
else
    exit $?
fi

which can be shortened to

可以缩短为?

startup.sh || exit $?
tail -f catalina.out

#2


0  

As an alternative, you might want to take a look at the Apache Tomcat Manager application. It supports, amongst other things:

作为另一种选择,您可能需要查看Apache Tomcat Manager应用程序。它支持其他事情:

  • Deploying applications remotely, and from local paths
  • 远程部署应用程序和本地路径。
  • Listing currently deployed applications
  • 清单目前部署的应用程序
  • Reloading existing applications
  • 现有应用程序重新加载
  • Starting an existing application
  • 从现有的应用程序
  • Stopping an existing application
  • 停止现有的应用程序
  • Undeploying an existing application
  • 取消部署现有应用程序

The manager provides a web interface that can be called via curl, and which returns simple, parseable messages to indicate the status of the invoked command. Management functions can also be invoked via JMX, or Ant scripts. All in all, a very handy tool.

manager提供了一个可以通过curl调用的web接口,它返回简单的、可解析的消息,以指示调用的命令的状态。管理功能也可以通过JMX或Ant脚本调用。总之,这是一个非常方便的工具。

#3


0  

I ended up implementing a solution using Python's subprocess.Popen, as suggested by @snies.

最后,我使用Python的子流程实现了一个解决方案。Popen,正如@snies所建议的。

Here's what it looks like:

下面是它的样子:

waitForIt.py

waitForIt.py

#! /usr/bin/env python
import subprocess
import sys

def main(argv):
    filename = argv[1]
    match=argv[2]
    p = subprocess.Popen(['tail', '-n', '0', '-f', filename], stdout=subprocess.PIPE)
    while True :
        line = p.stdout.readline()
        print line ,
        if match in line :
            break
    p.terminate()

if __name__ == "__main__":
    main(sys.argv)

tailUntil.sh

tailUntil.sh

#!/bin/bash
set -e

filename=$1
match=$2

thisdir=$(dirname $0)
python $thisdir/waitForIt.py "$filename" "$match"

and then

然后

startTomcat.sh

startTomcat.sh

${TOMCAT_HOME}/bin/startup.sh
logDeploy.sh "Agora vamos dar um tail no catalina.out..."
util_tailUntil.sh "$TOMCAT_HOME/logs/catalina.out" 'INFO: Server startup in '

It doesn't do what I originally intended (it still exits with return code 0 even when there is a stacktrace - but that could be changed with a little bit more of Python magic), but all of tomcat's initialization log is part of the automated deploy out (and easily viewable on Jenkins' deploy job) - so that's good enough.

它不做我最初(它仍然退出返回码为0,即使有一个加亮,但可以改变一点的Python魔法),但是所有的tomcat的初始化日志的一部分自动部署(和轻松可视詹金斯的部署工作)——这是不够好。

#1


0  

Maybe something like this?

也许是这样的?

tmp=$(mktemp -t catalina.XXXXXXX) || exit 136
trap 'rm "$tmp"' 0
trap 'exit 255' 2 15
tail -n 200 catalina.out >"$tmp"
if grep -q error "$tmp"; then
    cat "$tmp"
    exit 1
fi
exit 0

On the other hand, if startup.sh were competently coded, you could just

另一方面,如果创业。sh是很好的编码,你可以。

if startup.sh; then
    tail -f catalina.out
else
    exit $?
fi

which can be shortened to

可以缩短为?

startup.sh || exit $?
tail -f catalina.out

#2


0  

As an alternative, you might want to take a look at the Apache Tomcat Manager application. It supports, amongst other things:

作为另一种选择,您可能需要查看Apache Tomcat Manager应用程序。它支持其他事情:

  • Deploying applications remotely, and from local paths
  • 远程部署应用程序和本地路径。
  • Listing currently deployed applications
  • 清单目前部署的应用程序
  • Reloading existing applications
  • 现有应用程序重新加载
  • Starting an existing application
  • 从现有的应用程序
  • Stopping an existing application
  • 停止现有的应用程序
  • Undeploying an existing application
  • 取消部署现有应用程序

The manager provides a web interface that can be called via curl, and which returns simple, parseable messages to indicate the status of the invoked command. Management functions can also be invoked via JMX, or Ant scripts. All in all, a very handy tool.

manager提供了一个可以通过curl调用的web接口,它返回简单的、可解析的消息,以指示调用的命令的状态。管理功能也可以通过JMX或Ant脚本调用。总之,这是一个非常方便的工具。

#3


0  

I ended up implementing a solution using Python's subprocess.Popen, as suggested by @snies.

最后,我使用Python的子流程实现了一个解决方案。Popen,正如@snies所建议的。

Here's what it looks like:

下面是它的样子:

waitForIt.py

waitForIt.py

#! /usr/bin/env python
import subprocess
import sys

def main(argv):
    filename = argv[1]
    match=argv[2]
    p = subprocess.Popen(['tail', '-n', '0', '-f', filename], stdout=subprocess.PIPE)
    while True :
        line = p.stdout.readline()
        print line ,
        if match in line :
            break
    p.terminate()

if __name__ == "__main__":
    main(sys.argv)

tailUntil.sh

tailUntil.sh

#!/bin/bash
set -e

filename=$1
match=$2

thisdir=$(dirname $0)
python $thisdir/waitForIt.py "$filename" "$match"

and then

然后

startTomcat.sh

startTomcat.sh

${TOMCAT_HOME}/bin/startup.sh
logDeploy.sh "Agora vamos dar um tail no catalina.out..."
util_tailUntil.sh "$TOMCAT_HOME/logs/catalina.out" 'INFO: Server startup in '

It doesn't do what I originally intended (it still exits with return code 0 even when there is a stacktrace - but that could be changed with a little bit more of Python magic), but all of tomcat's initialization log is part of the automated deploy out (and easily viewable on Jenkins' deploy job) - so that's good enough.

它不做我最初(它仍然退出返回码为0,即使有一个加亮,但可以改变一点的Python魔法),但是所有的tomcat的初始化日志的一部分自动部署(和轻松可视詹金斯的部署工作)——这是不够好。