java 等到tomcat启动完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/376279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:08:35  来源:igfitidea点击:

Wait until tomcat finishes starting up

javalinuxtomcat

提问by timdisney

I have a script that needs to run after tomcat has finished starting up and is ready to start deploying applications. I'm using $TOMCAT_HOME/bin/startup.shwhich returns immediately. How can I wait until tomcat has finished starting up?

我有一个脚本需要在 tomcat 完成启动并准备开始部署应用程序后运行。我正在使用$TOMCAT_HOME/bin/startup.sh它立即返回。我怎样才能等到 tomcat 完成启动?

回答by Matt Solnit

There are probably several ways to do this. The trick we use is:

可能有几种方法可以做到这一点。我们使用的技巧是:

#!/bin/bash

until [ "`curl --silent --show-error --connect-timeout 1 -I http://localhost:8080 | grep 'Coyote'`" != "" ];
do
  echo --- sleeping for 10 seconds
  sleep 10
done

echo Tomcat is ready!

Hope this helps!

希望这可以帮助!

回答by daoway

It's not hard to implement programaticaly. You can implement org.apache.catalina.LifecycleListener and then you'll have

以编程方式实施并不难。你可以实现 org.apache.catalina.LifecycleListener 然后你就会有

public void lifecycleEvent(LifecycleEvent lifecycleEvent) {
            if(lifecycleEvent.getType().equals(Lifecycle.START_EVENT))
            //do what you want
            }       
}

in web.xml :

在 web.xml 中:

<Context path="/examples" ...>
...
<Listener className="com.mycompany.mypackage.MyListener" ... >
...
</Context>

Please notice that some things could differ between 6-9 Tomcats.

请注意,6-9 个 Tomcat 之间可能会有一些不同。

回答by The Governor

Are you still looking for an answer? It depends on your definition of started. If your definition of started is "Now its safe to stop" then you might want to verify if port 8005 is listening.

你还在寻找答案吗?这取决于你对开始的定义。如果您对开始的定义是“现在可以安全停止”,那么您可能需要验证端口 8005 是否正在侦听。

回答by Dustin

Depends on what you mean by finishing. What do you want to wait for?

取决于你所说的完成是什么意思。你想等什么?

You could, for example, have a script that hits a URL repeatedly until it gets a desirable result that would only be available once the app is properly initialized.

例如,您可以有一个脚本重复访问 URL,直到获得理想的结果,该结果只有在应用程序正确初始化后才可用。

You could also have a context listener that writes out an "I'm ready" file that you use to signal the readiness of your application. (If you do this, be sure the file doesn't exist before starting your app container).

您还可以有一个上下文侦听器,它会写出一个“我准备好了”的文件,您可以使用该文件来表示应用程序已准备就绪。(如果这样做,请确保在启动应用程序容器之前该文件不存在)。

回答by Mike

There isn't an easy method. As far as startup.sh and catalina.sh are concerned, tomcat is running when they finish. Although, internally, tomcat is still initializing and starting contexts.

没有简单的方法。就startup.sh 和catalina.sh 而言,tomcat 在它们完成时正在运行。尽管在内部,tomcat 仍在初始化和启动上下文。

It would help to know if you were trying to find out if your context finished loading or if you are just wanting a general, "Tomcat is runnnig although your contexts might not be completely loaded..."

如果您想知道您的上下文是否已完成加载,或者您只是想要一个一般信息,“Tomcat 正在运行,但您的上下文可能未完全加载......”,这将有助于了解

If it is the latter you could create a web app that simply has a context listener that will execute a script using Runtime. If you were handy, you could make the webapp configuable via the web.xml file to accept a parameter that points to the script to execute.

如果是后者,您可以创建一个仅具有上下文侦听器的 Web 应用程序,该侦听器将使用运行时执行脚本。如果您方便的话,您可以通过 web.xml 文件使 webapp 可配置,以接受指向要执行的脚本的参数。

回答by Loki

Personally, I would just watch catalinas log for a specific string depending on how your setup and what exact phase your looking for.

就个人而言,我只会根据您的设置方式和您寻找的确切阶段来查看特定字符串的 catalinas 日志。

回答by valentin_nasta

I needed this to test from jenkins if the tomcat from the remote server started for a system check.

我需要这个来从 jenkins 测试远程服务器的 tomcat 是否启动进行系统检查。

until [[ `ssh -o StrictHostKeyChecking=no root@${DEPLOY_HOST} 'netstat -tulpn | grep 8005'` != "" ]] ; do echo "waiting for tomcat"; sleep 6; done

until [[ `ssh -o StrictHostKeyChecking=no root@${DEPLOY_HOST} 'netstat -tulpn | grep 8005'` != "" ]] ; do echo "waiting for tomcat"; sleep 6; done

回答by sigi

I have done it with the following code in jenkins pipelinescript with tomcat. Before i just call

我在带有 tomcat 的 jenkins pipelinescript 中使用以下代码完成了它。在我打电话之前

sudo /bin/systemctl restart tomcat

And have an entry in my sudoers file for the jenkins user.

并在我的 sudoers 文件中为 jenkins 用户添加一个条目。

Now here is the oneliner:

现在这里是oneliner:

until [ "$(curl -w '%{response_code}' --no-keepalive -o /dev/null --connect-timeout 1 -u USERNAME:PASSWORD http://localhost:8080/manager/text/list)" == "200" ]; do echo --- sleeping for 1 second; sleep 1; done

Better readable:

更好的可读性:

until [ "$(curl -w '%{response_code}' --no-keepalive -o /dev/null --connect-timeout 1 -u USERNAME:PASSWORD http://localhost:8080/manager/text/list)" == "200" ];
do echo --- sleeping for 1 second;
sleep 1;
done