简体   繁体   English

等到tomcat完成启动

[英]Wait until tomcat finishes starting up

I have a script that needs to run after tomcat has finished starting up and is ready to start deploying applications. 我有一个脚本需要在tomcat完成启动后运行并准备开始部署应用程序。 I'm using $TOMCAT_HOME/bin/startup.sh which returns immediately. 我正在使用$TOMCAT_HOME/bin/startup.sh ,它会立即返回。 How can I wait until tomcat has finished starting up? 我怎么能等到tomcat完成启动?

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! 希望这可以帮助!

It's not hard to implement programaticaly. 实现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只雄猫之间的某些事情可能会有所不同。

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. 如果您对starts的定义是“现在可以安全停止”,那么您可能需要验证端口8005是否正在侦听。

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). (如果这样做,请确保在启动应用程序容器之前该文件不存在)。

There isn't an easy method. 没有一种简单的方法。 As far as startup.sh and catalina.sh are concerned, tomcat is running when they finish. 就startup.sh和catalina.sh而言,tomcat在完成时就会运行。 Although, internally, tomcat is still initializing and starting contexts. 虽然在内部,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. 如果是后者,您可以创建一个Web应用程序,它只有一个上下文侦听器,它将使用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.xml文件配置webapp,以接受指向要执行的脚本的参数。

就个人而言,我会根据你的设置和你想要的确切阶段来观察catalinas日志中的特定字符串。

如果来自远程服务器的tomcat启动进行系统检查,我需要从jenkins进行测试。

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

I have done it with the following code in jenkins pipelinescript with tomcat. 我已经使用tomcat中的jenkins pipelinescript中的以下代码完成了它。 Before i just call 在我打电话之前

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使Tomcat等到Web应用程序完成? - How to make Tomcat wait until web application finishes? 等到线程正常完成 - Wait until thread finishes gracefully GUI必须等到启动屏幕完成执行 - GUI has to wait until splashscreen finishes executing Java - 在 for 循环中等待直到方法完成,然后继续 - Java - wait in for loop until method finishes, then continue 如何等待直到异步任务中另一个类的方法在Android中完成 - How to wait until method from another class in asynctask finishes in android 直到完成某个Android应用程序(Java)为止 - Making an Android App (Java) 'Wait' Until Something Finishes 如何等到List <Mono<List<Object> &gt;&gt; 结束了吗? - How to wait until List<Mono<List<Object>>> finishes? 启动监听器时Apache Tomcat错误 - Apache Tomcat Error Upon Starting Up on Listener ExecutorService 应该等到一批 taksk 完成后再重新开始 - ExecutorService should wait until batch of taksk is finished before starting again 如何等到四个不同的方法完成,然后在 Android/Java 中执行另一个不同的方法 - How to wait until four different methods finishes and then execute another different method in Android/Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM