22

I'm writing a bash script that automatically deploys an application to a tomcat server. How can I stop the application from bash / command line?

Michael Küller
  • 3,982
  • 4
  • 22
  • 42

6 Answers6

13

Another way is to use CURL. In my case, I am on a corporate Windows machine that does not have WGET. I do have CURL though, and can use it via the GIT BASH terminal.

To list the applications running on Tomcat, I run the following (with user:password)

curl --user admin:admin http://localhost:8080/manager/text/list

Then to stop an application named "myapp"

curl --user admin:admin http://localhost:8080/manager/text/stop?path=/myapp
MattC
  • 5,874
  • 1
  • 47
  • 40
  • And if you have a parallel deployment, with the hashtag sign (#), remember that you must URL encode it to %23 in the path. – Erica Kane Feb 04 '19 at 22:17
12

The easiest method I know of is to install the Tomcat manager webapp, note the URL for stopping an application, and wget that URL.

Sam
  • 2,939
  • 19
  • 17
  • This is the correct answer: if you want to stop an individual webapp, then you'll have to use the internal APIs of Tomcat to do it or use JMX. The Manager webapp is all set up for everything you want to do, and it's easy to configure security for the manager webapp, too. – Christopher Schultz Jun 19 '12 at 16:14
  • 1
    My stop URL should contain "org.apache.catalina.filters.CSRF_NONCE" token. It complicates this approach. – Soid May 18 '13 at 05:12
  • Kotfu's answer better details this. – Haroldo_OK Jun 25 '14 at 13:59
12

Try this command-line script for managing tomcat called tomcat-manager. It requires Python, but allows you to do stuff from a Unix shell like:

$ tomcat-manager --user=admin --password=newenglandclamchowder \
> http://localhost:8080/manager/ stop /myapp

and:

$ tomcat-manager --user=admin --password=newenglandclamchowder \
> http://localhost:8080/manager deploy /myapp ~/src/myapp/myapp.war

Because it talks to tomcat over HTTP, it works "locally", i.e. via localhost, or from anywhere your tomcat instance is accessible.

kotfu
  • 161
  • 1
  • 3
9

I use wget to stop and start applications. The user in tomcat-user.xml must have manager-script roles.

For TOMCAT 5/6:

wget "http://<user>:<password>@<servername>:<port>/manager/stop?=/<application context>" -O - -q
wget "http://<user>:<password>@<servername>:<port>/manager/start?=/<application context>" -O - -q

Since TOMCAT 7 (7.0.62 for my installation) you have to add /text/ after manager:

wget "http://<user>:<password>@<servername>:<port>/manager/text/stop?path=/<application context>" -O - -q
wget "http://<user>:<password>@<servername>:<port>/manager/text/start?path=/<application context>" -O - -q
xav
  • 5,452
  • 7
  • 48
  • 57
3

There are three ways to stop tomcat application

  1. With local access you can of course just stop the process. This stops tomcat entirely
  2. With local and remote access you can access the "shutdown port", defined in server.xml (default=8005) alon with its password. If you open a socket to this and send the password, tomcat shuts down entirely.
  3. You follow sam's advice, which lets you be selective.
MJB
  • 9,352
  • 6
  • 34
  • 49
  • Stopping tomcat entirely is not my goal. I do want to be selective, because I don't want to stop any other applications that are running in parallel – Michael Küller Jun 19 '12 at 07:13
  • Then sam's advice is the correct one. Be aware that it's very easy for an app server to leak memory with undeploy/redeploy scenarios... – MJB Jun 19 '12 at 19:14
0

In some cases the pragmatic way can be to simply remove or rename the respective war-file, so tomcat undeploys the app.

Markus
  • 5,976
  • 5
  • 6
  • 21