Script of recommended for restarting multiple tomcat services under Linux

  • 2020-06-15 10:48:32
  • OfStack

tomcat's cache and redis's cache are so severe after modifying tomcat's configuration file or manually manipulating database data that tomcat needs to be restarted frequently to free the cache, often manually.


# 1 , find tomcat The process of ID
ps -ef | grep tomcat
# 2 , according to the path to find the process ID And then kill them one at a time (after all 1 There will be multiple different services on a server tomcat Running.) 
kill -9 [ID]

When I first encountered these commands, I would often manually type the commands and practice deepening them. Over time, it felt like a waste of time (sometimes changing database contents frequently).

So I wondered if I could make up an shell corner book to do all the meaningless work for me.

New file:


touch restart.sh
vi restart.sh

Document Contents:


#!/bin/bash
kow=/opt/apache-tomcat-7.0.54
open_acct=/opt/open_acct2
echo "killing tomcat..."
#  find tomcat The process of id And, kill off 
ps -ef | grep -v grep | grep -i '/opt/apache-tomcat-7.0.54\|/opt/open_acct2' | awk '{print $2}' | sed -e "s/^/kill -9 /g" | sh -
echo "killed tomcat"
echo "starting kow tomcat..."
#  Restart the tomcat
$kow/bin/startup.sh
$open_acct/tomcat2/bin/startup.sh
$open_acct/apache-tomcat-7.0.54/bin/startup.sh

Then save the file and add executable permissions:


chmod +x restart.sh

You can then restart multiple tomcat commands by running them directly.

Note: there are 1 part of sed and sh. I don't know what they mean after I check the documentation, but the general idea is to cache the process ID and then execute it as shell.

My idea is to match tomcat's execution path to find the deleted process I want,


# this 1 The section content matches multiple different values and can be modified to match the path you want 
'/opt/apache-tomcat-7.0.54\|/opt/open_acct2' 

Related articles: