Slime: Learn tomcat to bulk manage multiple tomcat through the shell

Source: Internet
Author: User
Tags apache tomcat egrep

This article is sponsored by Ilanniweb, starting in Slime Line the world

Want to get more articles, you can follow my ilanniweb

The company's business is to use Tomcat as a Web container, in order to more effectively utilize server performance, we typically deploy multiple tomcat to do business load balancing.

First, business needs

Currently, 4 Tomcat is deployed on a single server, which is equivalent to 4 nodes. Usually in the maintenance of business, we need a tomcat instance for a separate maintenance, feel very inconvenient, so there is this article.

But in order to manage these 4 tomcat more conveniently, I have done two things, one is to point all the Tomcat working directory to a separate directory, and the other is to start, close and restart Tomcat through the shell.

PS : The test OS for this experiment is centos6.5 64bit, but shell scripts can also be used under Ubuntu.

Ii. modification of the Tomcat working directory

In order not to have a separate maintenance tomcat instance, let's first modify the working directory of the Tomcat instance. All Tomcat configuration files are located in the Server.xml file under the Conf directory.

We only need to modify the AppBase configuration entry for the Server.xml file so that you can specify the working directory of the Tomcat instance. As follows:

We need to change each Tomcat instance server.xml configuration file appbase configuration item to/app/tomcat/apiwebapps.

When the above modifications are complete, we need to create the/app/tomcat/apiwebapps directory.


Tree-l 3/app/

Now we can look at Node1 as an example to see the directory structure of the Tomcat instance, as follows:

Tree-l 1./node1

Note: The ports that these 4 tomcat are listening on are different, or Tomcat cannot be started.

For a multi-instance of Tomcat, you can see my next article, "Slime: Learning Tomcat's tomcat single-machine multi-instance."

Iii. Creating shell Scripts

Now I put 4 tomcat instances into the/app/tomcat directory, the 4 Tomcat instance directory names are Node1, Node2, Node3, Node4. You can see that the naming rules for these 4 tomcat instances are node${i}. Now start writing shell scripts.

3.1 New Script

Create a new shell file named, which reads as follows:


# Apache Tomcat Daemon

# chkconfig:345 10 10

# Description:apache Tomcat Daemon

# Processname:tomcat

echo "___ __"

echo "/_ __/___ ____ ___ _________ _//_"

echo "///__ \ __ \ ___/__/__/"

echo "////_////////__//_///_"

echo "/_/\____/_//_//_/\___/\__,_/\__/"

echo ""


Export java_home=/usr/local/java/jdk1.7.0_80

#定义多个tomcat的总目录, there are four Tomcat instances Node1, Node2, Node3, and Node4 in the/app/tomcat directory.

Tom= "/app/tomcat/node"


Startup_bin= "Bin/"

#定义tomcat的启动方式, starting with a command similar to p1 start

Usage= "{P1|p2|p3|all} {start|stop|restart|status}"

dev= "/dev/null"

#定义如何启动tomcat, here we are using the number tomcat and the previously defined Tomcat command to manipulate Tomcat

#judge $ whether null

if ["=" = ""-o "$" = = ""];then


echo "Usage: $ $usage"


Exit 1


#judge $

Case $ in


tomcats= "1"



tomcats= "1 2"



Tomcats= "1 2 3"



Tomcats= "1 2 3 4"



echo "Usage: $ $usage"




#define START function

Tomcatstart () {

For I in $tomcats


Tom_home= "$tom $i"

run_status=$ (ps-ef | grep-v ' grep ' | egrep "Java.*=${tom_home}")

If ["${run_status}x"! = "X"];then

echo "Node$i is already running ..."


${tom_home}/${startup_bin} &> $dev

echo "node$i starting,please wait 2s ..."

Sleep 2





#define STOP function

Tomcatstop () {

For J in $tomcats


Tom1_home= "$tom $j"

tomcat_pid=$ (Ps-ef | grep ${tom1_home} | grep "Org.apache.catalina.startup.Bootstrap start" | awk ' {printf} ')

If ["${tomcat_pid}x" = = "X"];then

echo "Node$j is not running ..."


Kill-9 ${tomcat_pid} & > $dev

echo "node$j stopping,please wait 1s ..."

Sleep 1

echo "delte node$j cache,please wait 1s ..."

RM-RF ${tom1_home}/work/*





#define RESTART function

Tomcatrestart () {

For M in $tomcats


Tom2_home= "$tom $m"

run2_status=$ (ps-ef | grep-v ' grep ' | egrep "Java.*=${tom2_home}")

If ["${run2_status}x" = = "X"];then

echo "node$m is not running ..."

${tom2_home}/${startup_bin} &> $dev

echo "node$m starting,please wait 2s ..."

Sleep 2


Ps-ef | grep ${tom2_home} | grep "Org.apache.catalina.startup.Bootstrap Start" | awk ' {printf} ' | Xargs kill-9 > $dev

echo "node$m stopping,please wait 2s ..."

Sleep 1

${tom2_home}/${startup_bin} &> $dev

echo "node$m starting,please wait 2s ..."

Sleep 2





#define STATUS function

Tomcatstatus () {

For N in $tomcats


Tom3_home= "$tom $n"

run3_status=$ (ps-ef | grep-v ' grep ' | egrep "Java.*=${tom3_home}")

If ["${run3_status}x" = = "X"];then

echo "Node$n is not running ..."


echo "Node$n is running"





Case $ in














echo "Usage: $ $usage"



3.2 Modify file Permissions

After the script is created, let's now modify the execution permissions. As follows:

chmod +x

Four, start and close Tomcat

Once the permissions have been modified, we are ready to launch the 4 tomcat instances. For example, to start the two instances of Node1 and node2 now, we can start with the following command.

./ P2 Start

./ P2 Status

Through, it can be clearly seen that the instance Node1, Node2 has been started.

After Tomcat starts, if you want to stop, we can use the following command:

./ P2 Stop

Through, we can see that we have stopped the instance Node1, Node2, and we have also deleted the instance Node1, Node2 cache.

The above is based on the example of starting two Tomcat instances, and if starting or shutting down is one, three, and all Tomcat instances, we can use the following command.

./ P1 Start

./ P3 Start

./ all Start

Of course, the corresponding shutdown, restart and view status commands, respectively, are as follows.

Close command:

./ P1 Stop

./ P3 Stop

./ All Stop

To view the status command:

./ P1 Status

./ P3 Status

./ All Status

Note: The shell script can only follow Node1, Node2, Node3, NODE4, etc., and cannot stop and start the Tomcat instance separately, except Node1.

V. Adding Ilanni scripts to System services

For ease of operation, we can add the script to the system service. Here's how it works:



Chkconfig Ilanni on

To see if a list of services has been added

Chkconfig--list |grep Ilanni

Once added to the service, you can start and stop and restart the Tomcat instance in a service way. As follows:

/etc/init.d/ilanni P2 Start

/etc/init.d/ilanni P2 Status

Through, we can out, script has been added to the system service.

This is the end of the article about managing tomcat in bulk through the shell.

Slime: Learn tomcat to bulk manage multiple tomcat through the shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.