user3269829
user3269829

Reputation: 141

Restart remote tomcat using batch file to stop manual intervention to restart?

I would like to restart remote tomcat instance using batch file. Is it possible?

flow:

Stop tomcat

execute some sql script

start tomcat

Is it possible? If so, could you please give me some insight to achieve this?

Thanks!

Upvotes: 1

Views: 2893

Answers (3)

Aaron Levenstein
Aaron Levenstein

Reputation: 395

I wrote a ant build script which will restart tomcat and clear the tomcat cache. Just throw the xml file into tomcat/bin. The code used to wait for the server to stop doesn't seem to work on all systems so I just added a target that just waits for 3 minutes.

{code}

<property name="startServer.dir" value="." />
<property name="startServer.cmd.unix" value="startup.sh"/>
<property name="startServer.cmd.windows" value="startup.bat"/>
<property name="stopServer.cmd.unix" value="shutdown.sh"/>
<property name="stopServer.cmd.windows" value="shutdown.bat"/>
<property name="maven.port" value="8080"/>
<property name="deployed.cache" value="../work"/>


<!-- stop web server targets -->
<target name="stop" depends="" description="stop app server which is configured on this system">
    <echo message="Attempting to stop app server ${startServer.dir}"/>
    <echo message="${stopServer.cmd.unix} / ${stopServer.cmd.windows}"/>
    <exec dir="${startServer.dir}" osfamily="unix" executable="sh" timeout="18000">
        <arg line="${stopServer.cmd.unix}"/>
    </exec>
    <exec dir="${startServer.dir}" osfamily="windows" executable="cmd" timeout="18000">
        <arg line="/c ${stopServer.cmd.windows}"/>
    </exec>
    <echo message="waiting for server to stop"/>
    <waitfor maxwait="5" maxwaitunit="minute" checkevery="500">
        <not>
            <http url="http://localhost:${maven.port}"/>
        </not>
    </waitfor>
</target>

<target name="pause">
    <echo message="Pausing for 3 minutes to make sure server is stopped" />
    <sleep minutes="3"/>
</target>

<!-- start web server targets -->
<target name="start" description="start app server which is configured on this system">
    <echo message="Attempting to start app server server ${startServer.dir}"/>
    <echo message="${startServer.cmd.unix} / ${startServer.cmd.windows}"/>
    <exec dir="${startServer.dir}" osfamily="unix" executable="sh" spawn="true">
        <arg line="${startServer.cmd.unix}"/>
    </exec>
    <exec dir="${startServer.dir}" osfamily="windows" executable="cmd" spawn="true">
        <arg line="/c ${startServer.cmd.windows}"/>
    </exec>
</target>

<target name="cleanTomcat" description="Remove tomcat cashe">
    <delete dir="${deployed.cache}" verbose="true"/>
</target>

<target name="restart" depends="stop,pause,cleanTomcat,start"/> 

{code}

Upvotes: 0

Rahul
Rahul

Reputation: 3509

You can remotely deploy, start, stop and restart the tomcat. For this you will have to perform the following steps:

  1. To control the tomcat you can write the ant build script.
  2. Write the batch script and call the ant script to execute the desired target. such as stop tomcat.
  3. perform your desired task
  4. execute the target in ant script to start the tomcat again.

How to remotely operate the tomcat you can use the following link:

http://tomcat.apache.org/tomcat-7.0-doc/manager-howto.html

Upvotes: 0

AlexR
AlexR

Reputation: 115328

Sure it is possible. On the top of my head:

  1. You can control tomcat using scripts (startup, shutdown, catalina) that you can find in tomcat bin directory. The files extension depends on platform (.bat for windows, .sh for Unix
  2. To run these scripts remotely use ssh or telnet connection.
  3. You can also control tomcat using service manager. The tools depend on your platform.

Upvotes: 2

Related Questions