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.
Mkdir-p/app/tomcat/apiwebapps
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 ilanni.sh Script
Create a new shell file named Ilanni.sh, which reads as follows:
#!/bin/bash
# Apache Tomcat Daemon
# chkconfig:345 10 10
# Description:apache Tomcat Daemon
# Processname:tomcat
echo "___ __"
echo "/_ __/___ ____ ___ _________ _//_"
echo "///__ \ __ \ ___/__/__/"
echo "////_////////__//_///_"
echo "/_/\____/_//_//_/\___/\__,_/\__/"
echo ""
#定义JAVA_HOME
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/startup.sh"
#定义tomcat的启动方式, starting with a command similar to tomcat.sh 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
echo "Usage: $ $usage"
Echo
Exit 1
Fi
#judge $
Case $ in
"P1")
tomcats= "1"
;;
"P2")
tomcats= "1 2"
;;
"P3")
Tomcats= "1 2 3"
;;
"All")
Tomcats= "1 2 3 4"
;;
*)
echo "Usage: $ $usage"
;;
Esac
#定义tomcat的启动
#define START function
Tomcatstart () {
For I in $tomcats
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 2s ..."
Sleep 2
Fi
Done
}
#定义tomcat的关闭
#define STOP function
Tomcatstop () {
For J in $tomcats
Do
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 ..."
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 ${tom1_home}/work/*
Fi
Done
}
#定义tomcat的重启
#define RESTART function
Tomcatrestart () {
For M in $tomcats
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 2s ..."
Sleep 2
Else
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
Fi
Done
}
#定义tomcat的状态
#define STATUS function
Tomcatstatus () {
For N in $tomcats
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
Case $ in
"Start")
Tomcatstart
;;
"Stop")
Tomcatstop
;;
"Restart")
Tomcatrestart
;;
"Status")
Tomcatstatus
;;
*)
echo "Usage: $ $usage"
;;
Esac
3.2 Modify ilanni.sh file Permissions
After the ilanni.sh script is created, let's now modify the ilanni.sh execution permissions. As follows:
chmod +x ilanni.sh
Four, start and close Tomcat
Once the ilanni.sh 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.
./ilanni.sh P2 Start
./ilanni.sh 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:
./ilanni.sh 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.
./ilanni.sh P1 Start
./ilanni.sh P3 Start
./ilanni.sh all Start
Of course, the corresponding shutdown, restart and view status commands, respectively, are as follows.
Close command:
./ilanni.sh P1 Stop
./ilanni.sh P3 Stop
./ilanni.sh All Stop
To view the status command:
./ilanni.sh P1 Status
./ilanni.sh P3 Status
./ilanni.sh 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 ilanni.sh script to the system service. Here's how it works:
CP Ilanni.sh/etc/init.d/ilanni
Chkconfig--add/etc/init.d/ilanni
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, ilanni.sh 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