129

Interested to know how people usually check to see if Tomcat is running on a Unix environment.

I either check that the process is running using

ps -ef | grep java
ps -ef | grep logging

or i check that the port number is active

netstat -a | grep 8080

is there a better way of checking that Tomcat is running? The above seem to be to be a 'hacky' way of checking that Tomcat is running.

ziggy
  • 15,319
  • 64
  • 187
  • 280

17 Answers17

103

On my linux system, I start Tomcat with the startup.sh script. To know whether it is running or not, i use

ps -ef | grep tomcat  

If the output result contains the whole path to my tomcat folder, then it is running

Thai Tran
  • 9,607
  • 7
  • 41
  • 62
72

try this instead and because it needs root privileges use sudo

sudo service tomcat7 status
eeadev
  • 3,512
  • 7
  • 38
  • 93
Kemboi
  • 729
  • 5
  • 4
61

Why grep ps, when the pid has been written to the $CATALINA_PID file?

I have a cron'd checker script which sends out an email when tomcat is down:

kill -0 `cat $CATALINA_PID` > /dev/null 2>&1
if [ $? -gt 0 ]
then
    echo "Check tomcat" | mailx -s "Tomcat not running" support@dom.com
fi

I guess you could also use wget to check the health of your tomcat. If you have a diagnostics page with user load etc, you could fetch it periodically and parse it to determine if anything is going wrong.

erip
  • 15,290
  • 10
  • 62
  • 113
dogbane
  • 254,755
  • 72
  • 386
  • 405
  • is this as easy to do with windows? would i just modify the commands for windows equivalents, throw it in a batch file, and then schedule through scheduler? – liltitus27 Apr 28 '14 at 19:01
  • You could do something along the lines of `tasklist |find /I "Tomcat" || echo "Tomcat not running"` in a bat file, but you may need to find java, not Tomcat depending on what the process name is. – GL2014 Apr 28 '14 at 23:46
  • 3
    To learn what the value of $CATALINA_PID is from your OS-installed Tomcat, try: `ps aewwx | grep CATALINA_PID` and you will find it in the output for the running process, amongst the other environment variables. In Debian 8 this value is: CATALINA_PID=/var/run/tomcat8.pid – Professor Falken Jul 04 '16 at 00:26
  • 3
    It's important to note that a pid file may not exist, it depends on the environment and how tomcat was started. See [this answer](http://stackoverflow.com/a/22021196/120794) to create a pid file on startup. – Alberto de Paola Sep 25 '16 at 01:59
15

netstat -lnp | grep 8080 would probably be the best way, if you know Tomcat's listening port. If you want to be certain that is is functional, you will have to establish a connection and send an HTTP request and get a response. You can do this programatically, or using any web browser.

fluffyBatman
  • 6,004
  • 3
  • 21
  • 24
Michael Goldshteyn
  • 68,941
  • 23
  • 129
  • 179
  • IF you know the port, I think this is the simplest way given that there are variations in how you check if the executable is running depending on how you installed Tomcat, whether its a service or not etc. – Jaffer Oct 18 '18 at 10:51
13

You can check the status of tomcat with the following ways:

ps -ef | grep tomcat  

This will return the tomcat path if the tomcat is running

netstat -a | grep 8080

where 8080 is the tomcat port

Dhanish Jose
  • 743
  • 1
  • 8
  • 19
7

If tomcat is installed locally, type the following url in a browser window: { localhost:8080 }

This will display Tomcat home page with the following message.

If you're seeing this, you've successfully installed Tomcat. Congratulations!

If tomcat is installed on a separate server, you can type replace localhost by a valid hostname or Iess where tomcat is installed.

The above applies for a standard installation wherein tomcat uses the default port 8080

Lokanath
  • 103
  • 1
  • 1
  • 20
    If Tomcat is installed on a separate server, failing to access it from a browser won't tell you for sure whether it's running or not: it may be unreachable. – Marco Lackovic Mar 15 '13 at 17:40
  • I have installed Tomcat8 on Ubuntu, but going to http://localhost:8080/ results in the "Unable to connect" message. – ibodi Aug 09 '18 at 12:18
  • 1
    If you have a headless server on Linux this is not an option. – Klaus Nji Oct 10 '18 at 21:48
7

Create a Shell script that checks if tomcat is up or down and set a cron for sh to make it check every few minutes, and auto start tomcat if down. Sample Snippet of code below

TOMCAT_PID=$(ps -ef | awk '/[t]omcat/{print $2}')
echo TOMCAT PROCESSID $TOMCAT_PID

if [ -z "$TOMCAT_PID" ]
then
    echo "TOMCAT NOT RUNNING"
    sudo /opt/tomcat/bin/startup.sh
else
   echo "TOMCAT RUNNING"
fi
Devendra Dora
  • 547
  • 6
  • 9
  • I created a shell script that always return "TOMCAT RUNNING" even I stopped tomcat. But when I run the command on console, it worked fine. – Charles PHAM Mar 11 '19 at 03:18
  • Make sure that shell script name doesn't as tomcat keyword .If script has tomcat name , then in process list shell script name would show up , the code would think as if tomcat running . You can give any name to script without tomcat keyword in it – Devendra Dora May 05 '19 at 08:15
6

I always do

tail -f logs/catalina.out

When I see there

INFO: Server startup in 77037 ms

then I know the server is up.

konkit
  • 329
  • 6
  • 9
6

wget url or curl url where url is a url of the tomcat server that should be available, for example: wget http://localhost:8080. Then check the exit code, if it's 0 - tomcat is up.

Dikla
  • 3,419
  • 5
  • 29
  • 42
4

I've found Tomcat to be rather finicky in that a running process or an open port doesn't necessarily mean it's actually handling requests. I usually try to grab a known page and compare its contents with a precomputed expected value.

Alex Howansky
  • 47,154
  • 8
  • 74
  • 95
2

Since my tomcat instances are named as tomcat_ . For example. tomcat_8086, I use

#

ps aux | grep tomcat

Other method is using nc utility

nc -l 8086 (port number )

Or

ps aux | grep java

Himanshu Chauhan
  • 692
  • 8
  • 10
2

Are you trying to set up an alert system? For a simple "heartbeat", do a HTTP request to the Tomcat port.

For more elaborate monitoring, you can set up JMX and/or SNMP to view JVM stats. We run Nagios with the SNMP plugin (bridges to JMX) to check Tomcat memory usage and request thread pool size every 10-15 minutes.

http://tomcat.apache.org/tomcat-6.0-doc/monitoring.html

Update (2012):

We have upgraded our systems to use "monit" to check the tomcat process. I really like it. With very little configuration it automatically verifies the service is running, and automatically restarts if it is not. (sending an email alert). It can integrate with the /etc/init.d scripts or check by process name.

Will Glass
  • 4,710
  • 6
  • 31
  • 43
2

Try this command

ps -ef | awk '/[t]omcat/{print $2}' 

It will return the pid if tomcat is running.

Alin
  • 316
  • 1
  • 3
  • 8
1

tomcat.sh helps you know this easily.

tomcat.sh usage doc says:

no argument: display the process-id of the tomcat, if it's running, otherwise do nothing

So, run command on your command prompt and check for pid:

$ tomcat.sh

Wade
  • 435
  • 5
  • 6
Techflash
  • 609
  • 7
  • 12
0
$ sudo netstat -lpn |grep :8080

To check the port number

$ ps -aef|grep tomcat

Is any tomcat is running under the server.

tsssinfotech-K53U infotech # ps -aef|grep tomcat

root 9586 9567 0 11:35 pts/6 00:00:00 grep --colour=auto tomcat

Dovydas Šopa
  • 2,252
  • 8
  • 25
  • 32
0

Basically you want to test

  1. Connectivity to your tomcat instance
  2. Gather some basic statistics
  3. Whether the unix process is running

I will evaluate first 2 options as the 3rd one has been sufficiently answered already.

easiest is just to develop a webpage on your WebApp that gathers some basic metrics, and have a client that can read the results or detect connectivity issues.

For doing so, you have several issues

YoYo
  • 8,651
  • 8
  • 56
  • 70
0

Here are my two cents.

I have multiple tomcat instances running on different ports for my cluster setup. I use the following command to check each processes running on different ports.

/sbin/fuser 8080/tcp

Replace the port number as per your need.

And to kill the process use -k in the above command.

  • This is much faster than the ps -ef way or any other commands where you call a command and call another grep on top of it.
  • Works well with multiple installations of tomcat ,Or any other server that uses a port as a matter of fact running on the same server.

The equivalent command on BSD operating systems is fstat

Raja Anbazhagan
  • 3,412
  • 1
  • 38
  • 57