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.sh
which returns immediately. How can I wait until tomcat has finished starting up?
我有一个脚本需要在tomcat完成启动后运行并准备开始部署应用程序。我正在使用$ TOMCAT_HOME / bin / startup.sh,它会立即返回。我怎么能等到tomcat完成启动?
8 个解决方案
#1
36
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!
希望这可以帮助!
#2
8
It's not hard to implement programaticaly. You can implement org.apache.catalina.LifecycleListener and then you'll have
实现programaticaly并不难。你可以实现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只雄猫之间的某些事情可能会有所不同。
#3
2
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是否在监听。
#4
1
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).
您还可以使用一个上下文侦听器来写出“我已准备好”的文件,用于表示应用程序的准备情况。 (如果这样做,请确保在启动应用程序容器之前该文件不存在)。
#5
0
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应用程序,它只有一个上下文侦听器,它将使用Runtime执行脚本。如果您很方便,可以通过web.xml文件配置webapp,以接受指向要执行的脚本的参数。
#6
0
Personally, I would just watch catalinas log for a specific string depending on how your setup and what exact phase your looking for.
就个人而言,我会根据你的设置和你想要的确切阶段来观察catalinas日志中的特定字符串。
#7
0
I needed this to test from jenkins if the tomcat from the remote server started for a system check.
如果来自远程服务器的tomcat启动进行系统检查,我需要从jenkins进行测试。
until [[ `ssh -o StrictHostKeyChecking=no root@${DEPLOY_HOST} 'netstat -tulpn | grep 8005'` != "" ]] ; do echo "waiting for tomcat"; sleep 6; done
直到[[`ssh -o StrictHostKeyChecking = no root @ $ {DEPLOY_HOST}'netstat -tulpn | grep 8005'`!=“”]];回声“等待tomcat”;睡6; DONE
#8
0
I have done it with the following code in jenkins pipelinescript with tomcat. Before i just call
我已经使用tomcat中的jenkins管道脚本中的以下代码完成了它。在我打电话之前
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
#1
36
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!
希望这可以帮助!
#2
8
It's not hard to implement programaticaly. You can implement org.apache.catalina.LifecycleListener and then you'll have
实现programaticaly并不难。你可以实现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只雄猫之间的某些事情可能会有所不同。
#3
2
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是否在监听。
#4
1
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).
您还可以使用一个上下文侦听器来写出“我已准备好”的文件,用于表示应用程序的准备情况。 (如果这样做,请确保在启动应用程序容器之前该文件不存在)。
#5
0
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应用程序,它只有一个上下文侦听器,它将使用Runtime执行脚本。如果您很方便,可以通过web.xml文件配置webapp,以接受指向要执行的脚本的参数。
#6
0
Personally, I would just watch catalinas log for a specific string depending on how your setup and what exact phase your looking for.
就个人而言,我会根据你的设置和你想要的确切阶段来观察catalinas日志中的特定字符串。
#7
0
I needed this to test from jenkins if the tomcat from the remote server started for a system check.
如果来自远程服务器的tomcat启动进行系统检查,我需要从jenkins进行测试。
until [[ `ssh -o StrictHostKeyChecking=no root@${DEPLOY_HOST} 'netstat -tulpn | grep 8005'` != "" ]] ; do echo "waiting for tomcat"; sleep 6; done
直到[[`ssh -o StrictHostKeyChecking = no root @ $ {DEPLOY_HOST}'netstat -tulpn | grep 8005'`!=“”]];回声“等待tomcat”;睡6; DONE
#8
0
I have done it with the following code in jenkins pipelinescript with tomcat. Before i just call
我已经使用tomcat中的jenkins管道脚本中的以下代码完成了它。在我打电话之前
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