Rotten mud: Learn how to manage multiple tomcat in batches through shell, tomcatshell

Source: Internet
Author: User
Tags apache tomcat egrep

Rotten mud: Learn how to manage multiple tomcat in batches through shell, tomcatshell

This document consistsIlanniwebProviding friendship sponsorship, first launchedThe world

For more articles, follow my ilanniweb

The company's business is to use tomcat for web containers. To make better use of server performance, we generally deploy multiple tomcat servers for load balancing.

I. Business Requirements

Currently, four tomcat servers are deployed on one server, which is equivalent to four nodes. During business maintenance, we need to perform separate maintenance for one tomcat instance, which is inconvenient, so this article is available.

However, in order to manage these four tomcat servers more conveniently, I have done two jobs here. One is to direct all tomcat working directories to a separate directory, second, start, shut down, and restart tomcat through shell.

PS: The OS tested in this experiment is centos6.5 64bit, but the shell script can also be used in ubuntu.

2. Modify the tomcat working directory

To maintain tomcat instances separately, first modify the working directory of the tomcat instance. All tomcat configuration files are located in the server. xml file under the conf directory.

You only need to modify the appBase configuration item of the server. xml file to specify the working directory of the tomcat instance. As follows:

We need to change the appBase configuration item of the server. xml configuration file of each tomcat instance to/app/tomcat/apiwebapps.

After the preceding modifications, we need to create the/app/tomcat/apiwebapps directory.

Mkdir-p/app/tomcat/apiwebapps

Tree-L 3/app/

Now we can view node1 as an example to view the directory structure of the tomcat instance, as shown below:

Tree-L 1./node1

Note: The ports listened to by the four tomcat servers are different; otherwise, tomcat cannot be started.

For more information about single-host and multi-instance tomcat, see my next article "mud: learning about tomcat single-host and multi-instance".

3. Create shell scripts

Now I want to put all four tomcat instances under the/app/tomcat directory. These four tomcat instance directories are named node1, node2, node3, and node4. We can see that the naming rules for the four tomcat instances are node $ {I }. Now you can write shell scripts.

3.1Create an ilanni. sh script

Create a new shell file named ilanni. sh with the following content:

#! /Bin/bash

# Apache Tomcat daemon

# Chkconfig: 345 10 10

# Description: Apache Tomcat daemon

# Processname: tomcat

Echo "________"

Echo "/___/____________________//_"

Echo "///__\/____\/___/__/__/"

Echo "////_////////__//_///_"

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

Echo ""

# Define JAVA_HOME

Export JAVA_HOME =/usr/local/java/jdk1.7.0 _ 80

# Define the total directories of multiple tomcat servers. The/app/tomcat directory contains four tomcat instances: node1, node2, node3, and node4.

Tom = "/app/tomcat/node"

# Define the startup script path

Startup_bin = "bin/startup. sh"

# Define the tomcat startup mode. The startup mode is a command similar to tomcat. sh p1 start.

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

Dev = "/dev/null"

# Define how to start tomcat. Here we use the number of tomcat commands and the previously defined tomcat commands to operate tomcat

# Judge $1 $2 whether null

If ["$1" = ""-o "$2" = "]; then

Echo

Echo "Usage: $0 $ usage"

Echo

Exit 1

Fi

# Judge $1

Case $1 in

"P1 ")

Specified ATS = "1"

;;

"P2 ")

Specified ATS = "1 2"

;;

"P3 ")

Specified ATS = "1 2 3"

;;

"All ")

Using ATS = "1 2 3 4"

;;

*)

Echo "Usage: $0 $ usage"

;;

Esac

# Define tomcat startup

# Define start function

Tomcatstart (){

For I in $ Alibaba ATS

Do

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 ..."

Else

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

Echo "node $ I starting, Please wait 2 S ..."

Sleep 2

Fi

Done

}

# Define tomcat Shutdown

# Define stop function

Tomcatstop (){

For j in $ Alibaba ATS

Do

Tomdeskhome = "$ tom $ j"

Tomcat_pid =$ (ps-ef | grep $ {tom1_home} | grep "org. apache. catalina. startup. Bootstrap start" | awk '{printf $2 }')

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

Echo "node $ j is not running ..."

Else

Kill-9 $ {tomcat_pid} &> $ dev

Echo "node $ j stopping, Please wait 1s ..."

Sleep 1

Echo "delte node $ j cache, Please wait 1s ..."

Rm-rf $ {tomdomainhome}/work /*

Fi

Done

}

# Define tomcat restart

# Define restart function

Tomcatrestart (){

For m in $ sort ATS

Do

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 2 S ..."

Sleep 2

Else

Ps-ef | grep $ {tom2_home} | grep "org. apache. catalina. startup. Bootstrap start" | awk '{printf $2}' | xargs kill-9> $ dev

Echo "node $ m stopping, Please wait 2 S ..."

Sleep 1

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

Echo "node $ m starting, Please wait 2 S ..."

Sleep 2

Fi

Done

}

# Define tomcat status

# Define status function

Tomcatstatus (){

For n in $ Alibaba ATS

Do

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 ..."

Else

Echo "node $ n is running"

Fi

Done

}

# Judge $2

Case $2 in

"Start ")

Tomcatstart

;;

"Stop ")

Tomcatstop

;;

"Restart ")

Tomcatrestart

;;

"Status ")

Tomcatstatus

;;

*)

Echo "Usage: $0 $ usage"

;;

Esac

3.2Modify ilanni. sh File Permissions

After the ilanni. sh script is created, modify the execution permission of ilanni. sh. As follows:

Chmod + x ilanni. sh

4. Start and close tomcat

After the ilanni. sh permission is modified, we can start the four tomcat instances. For example, to start node1 and node2 instances, run the following command.

./Ilanni. sh p2 start

./Ilanni. sh p2 status

We can see that the instances node1 and node2 have been started.

To stop tomcat after it is started, run the following command:

./Ilanni. sh p2 stop

We can see that we have stopped node1 and node2 instances, and deleted the cache of node1 and node2.

The preceding example is to start two tomcat instances. If one or three tomcat instances and all tomcat instances are started or closed, run the following command.

./Ilanni. sh p1 start

./Ilanni. sh p3 start

./Ilanni. sh all start

Of course, the corresponding commands for disabling, restarting, and viewing status are as follows.

Close command:

./Ilanni. sh p1 stop

./Ilanni. sh p3 stop

./Ilanni. sh all stop

Command to view the status:

./Ilanni. sh p1 status

./Ilanni. sh p3 status

./Ilanni. sh all status

Note: This shell script can only be executed in the order of node1, node2, node3, and node4. You cannot separately stop and start tomcat instances except node1.

5. Add the ilanni script to the System Service

To facilitate the operation, we can add the ilanni. sh script to the system service. The procedure is as follows:

Cp ilanni. sh/etc/init. d/ilanni

Chkconfig -- add/etc/init. d/ilanni

Chkconfig ilanni on

Check whether the service has been added to the service list

Chkconfig -- list | grep ilanni

After being added to the service, you can start and stop the tomcat instance and restart the tomcat instance as a service. As follows:

/Etc/init. d/ilanni p2 start

/Etc/init. d/ilanni p2 status

Through this, we can see that the ilanni. sh script has been added to the system service.

This article is about managing tomcat in batches through shell.

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: info-contact@alibabacloud.com 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.