shell script to kill tomcat service if it is not stopped by stop command after certain amount of time?
./bin/catalina.sh
should support this. If you run that command without any options, it will print out its usage, which describes:
stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running
In order to make this work, you need to set the environment variable CATALINA_PID
to a file name that will be used to hold the Tomcat process ID. To start Tomcat, use:
export CATALINA_PID=/tmp/catalina.pid
./bin/catalina.sh start
And then to stop it:
export CATALINA_PID=/tmp/catalina.pid
./bin/catalina.sh stop 600 -force
This will try to stop it, wait for 5 minutes, then kill it if necessary. Note that this will run in the foreground by default (locking up the terminal instance); use a trailing &
to run the command in the background.
Kill tomcat service running on any port, Windows
1) Go to (Open) Command Prompt (Press Window + R then type cmd Run this).
2) Run following commands
For all listening ports
netstat -aon | find /i "listening"
Apply port filter
netstat -aon |find /i "listening" |find "8080"
Finally with the PID we can run the following command to kill the process
3) Copy PID from result set
taskkill /F /PID
Ex: taskkill /F /PID 189
Sometimes you need to run Command Prompt with Administrator privileges
Done !!! you can start your service now.
shell script to Start/Stop tomcat server
Please follow below step:
- Save provided code in
.sh
file (i.e. StartStopScript.sh) at one location. - Update
export BASE
variable with you tomcat bin location. - Update
prog
variable with tomcat version. - IMPORTANT: run the script with argument eg.
StartStopScript.sh start
to start the server
StartStopScript.sh stop
to stop the server
StartStopScript.sh restart
to restart the server
StartStopScript.sh status
to check server status
Tomcat doesn't stop. How can I debug this?
If the web application is stopped, all connections to the database should be closed as well. If you don't have a list of connections, then execute the SQL statement "shutdown" (this only works for the H2 and HSQLDB databases).
If you have a registered a Servlet, you can do that in the Servlet.destroy()
method.
If you have registered a ServletContextListener
, you can execute the "shutdown" statement in the ServletContextListener.contextDestroyed(ServletContextEvent servletContextEvent)
method. This is what org.h2.server.web.DbStarter
ServletContextListener
does (the one that is included in the H2 database).
Tomcat won't stop or restart
It seems Tomcat was actually stopped. I started it and it started fine. Thanks all.
Bash script: ensure process has terminated without waiting unnecessarily
The same method that you use for detecting the process is still running should be usable to see if it's not.
Something like this should do it, keeping in mind the possibility that there may be potential problems with that way of detecting if the program is running in the first place (a):
sh $PROGRAMX_HOME/server/bin/shutdown.sh 2>/dev/null
# Get initial PID and set limit.
PROCESS_ID=$(ps -ef | grep '[p]rogramx' | awk '{print $2}')
((LIMIT = 120))
# Loop every second, until process is gone or timer expires.
while [[ "x$PROCESS_ID" != "x" ]] ; do
sleep 1
PROCESS_ID=$(ps -ef | grep '[p]rogramx' | awk '{print $2}')
((LIMIT = LIMIT - 1))
if [[ $LIMIT -eq 0 ]] ; then
PROCESS_ID=""
fi
done
PROCESS_ID=$(ps -ef | grep '[p]rogramx' | awk '{print $2}')
if [ "x$PROCESS_ID" != "x" ]
then
kill -6 $PROCESS_ID >/dev/null 2>&1
fi
It's basically a loop which runs for up to 120 seconds (roughly) but stops prematurely if the process disappears, waiting at most a second or so after the process actually exits.
(a) A better way of detecting the process (without trying to parse the output of ps
) would be to use kill -0
which doeesn't actually send a signal to the process, it just returns a result stating whether the process exists.
Incorporating that kill -0
method would give you code like:
# Get process ID up front.
PROCESS_ID=$(ps -ef | grep '[p]rogramx' | awk '{print $2}')
# Only act if it's running.
if kill -0 $PROCESS_ID >/dev/null 2>&1; then
sh $PROGRAMX_HOME/server/bin/shutdown.sh 2>/dev/null
((LIMIT = 120))
# Loop every second, until process is gone or timer expires.
while kill -0 $PROCESS_ID >/dev/null 2>&1 && [[ $LIMIT -gt 0 ]] ; do
sleep 1
((LIMIT = LIMIT - 1))
done
# Be more forceful if it's still up.
if kill -0 $PROCESS_ID >/dev/null 2>&1 ; then
kill -6 $PROCESS_ID >/dev/null 2>&1
fi
fi
You'll also notice that I've changed your 2>&1 >/dev/null
into >/dev/null 2>&1
. If you want to throw away all messages, that's important - the order on the command line has subtle effects.
Related Topics
When Installing Rust Toolchain in Docker, Bash 'Source' Command Doesn't Work
Asp Net Core Linux Err_Connection_Refused
Gatttool Non-Interactive Mode --Char-Write
R Package Installation in Linux
Jupyter Lab - Suppress Console Output
Output File Not Created When Reading Sequences
How to Redirect Cout to Console in Linux
Assembly Call Subprograms Based on User Input
How to Escape the Bang (!) Character in Linux Bash Shell
Netcat Tcp Programming with Bash
Imagemagick: Generate Raw Image Data for PDF Flate Embedding
Unix How to Block Unix/Linux 'Wall' Messaging
Print the Directory Where the 'Find' Linux Command Finds a Match
Linux Randomly Deleted My File While Compiling What Do I Do
Iterating Over File (And Directory) Names with Bash
Segmentation-Fault Error Happening with Assembly Code Program