Check if Tomcat works through shell script

I need to check if Tomcat is working on my system using a shell script. If not, I need to catch the process id and kill Tomcat. How will this be achieved?

+4
source share
4 answers

to get the current process, I used this command:

ps x | grep [full_path_to_tomcat] | grep -v grep | cut -d ' ' -f 1

However, you must be careful. It works on my setup, but it may not work everywhere ... I have two tomcat installations, one of which is / usr / local / tomcat on port 8080 and / usr / local / tomcat _8081 on port 8081. I need to use '/ usr / local / tomcat /' (with a trailing slash) as full_path, because otherwise it will return 2 different pids if tomcat_8081 also works.

This explains what this command does:

1) ps x gives you a list of running processes sorted by pid, tty, stat, time running and command.

2) Applying grep [full_path_to_tomcat] to it, it will find the template [full_path_to_tomcat] in this list. For example, starting ps x | grep /usr/local/tomcat/ ps x | grep /usr/local/tomcat/ may result in the following:

 13277 ? Sl 7:13 /usr/local/java/bin/java -Djava.util.logging.config.fil e=/usr/local/tomcat/conf/logging.properties [...] -Dcatalina.home=/usr/local/tomca t [...] 21149 pts/0 S+ 0:00 grep /usr/local/tomcat/ 

3) Since we get 2 entries instead of one due to grep /usr/local/tomcat/ matching the pattern, delete it. -v is the inverted match flag for grep, which means that it will select only lines that do not match the pattern. So in the previous example, using ps -x | grep /usr/local/tomcat/ | grep -v grep ps -x | grep /usr/local/tomcat/ | grep -v grep ps -x | grep /usr/local/tomcat/ | grep -v grep will return:

 13277 ? Sl 7:13 /usr/local/java/bin/java -Djava.util.logging.config.fil e=/usr/local/tomcat/conf/logging.properties [...] -Dcatalina.home=/usr/local/tomca t [...] 

4) Cool, now we have the pid that we need. However, we need to remove the rest. To do this, use cut . This command removes partitions from a FILE or standard output. The -d option is the separator, and the -f is the field you need. Excellent. Therefore, we can use a space ('') as a separator and get the first field that matches pid. Running ps x | grep /usr/local/tomcat/ | grep -v grep | cut -d ' ' -f 1 ps x | grep /usr/local/tomcat/ | grep -v grep | cut -d ' ' -f 1 ps x | grep /usr/local/tomcat/ | grep -v grep | cut -d ' ' -f 1 will return:

 13277 

That's what you need. To use it in a script, it is simple:

 #replace below with your tomcat path tomcat_path=/users/tomcat/apache-tomcat-8.0.30 pid=$(ps x | grep "${tomcat_path}" | grep -v grep | cut -d ' ' -f 1) if [ "${pid}" ]; then eval "kill ${pid}" fi 
+8
source

One way to check with wget is for your server address and status check.

Check out this link here:

http://www.velvettools.com/2013/07/shell-script-to-check-tomcat-status-and.html#.VX_jfVz-X1E

 TOMCAT_HOME=/usr/local/tomcat-folder/ is_Running () { wget -O - http://yourserver.com/ >& /dev/null if( test $? -eq 0 ) then return 0 else return 1 fi } stop_Tomcat () { echo "shutting down......" $TOMCAT_HOME/bin/shutdown.sh } start_Tomcat () { echo "starting......" $TOMCAT_HOME/bin/startup.sh } restart () { stop_Tomcat sleep 10 kill_Hanged_Processes start_Tomcat sleep 60 } 
+4
source

easy way to do this:

  ps -ef | grep tomcat 

using this command, you will get:

 user [id-to-kill] Date [tomcat-path] 

Final step kills the process

 sudo kill -9 [id-to-kill] 

Congratulations, your process has been killed lOol

+1
source

The default port of Tomcat is 8080. u can grep it and use the port status in the comparison loop.

 #!/bin/bash STAT=`netstat -na | grep 8080 | awk '{print $7}'` if [ "$STAT" = "LISTEN" ]; then echo "DEFAULT TOMCAT PORT IS LISTENING, SO ITS OK" elif [ "$STAT" = "" ]; then echo "8080 PORT IS NOT IN USE SO TOMCAT IS NOT WORKING" ## only if you defined CATALINA_HOME in JAVA ENV ## cd $CATALINA_HOME/bin ./startup.sh fi RESULT=`netstat -na | grep 8080 | awk '{print $7}' | wc -l` if [ "$RESULT" = 0 ]; then echo "TOMCAT PORT STILL NOT LISTENING" elif [ "$RESULT" != 0 ]; then echo "TOMCAT PORT IS LISTENINS AND SO TOMCAT WORKING" fi 

this way you can compare script.you grep port 8080 if you use the default port for tomcat.this will check if tomcat is working. then you can check the processes using the port lsof -i: 8080 // if port 8080 is used

if you want to free a port by killing a process using it, use this kill 75782 command // if, for example, 75782 is a process using a port

0
source

All Articles