ziggy
ziggy

Reputation: 15876

Is Tomcat running?

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.

Upvotes: 134

Views: 674062

Answers (18)

YoYo
YoYo

Reputation: 9405

Other than basic unix network, service, or process commands, you can also try to run an actual health check on your tomcat instance. You can setup a basic dynamic webpage that generates some expected output, or even develop a Web API. Either one could be accessed through a simple unix curl command.

This will allow you to get an actual heartbeat of your application running on tomcat (could detect issues like exhausted connection pools etc).

There is also this - https://stackoverflow.com/a/68526209/744133

Upvotes: -1

Borea Deitz
Borea Deitz

Reputation: 505

This answer applies to Tomcat running as a service on recent versions of Ubuntu. One benefit of a service is that it provides tools for getting clear and unambiguous information on the status of the program.

If Tomcat is running as a systemd service, which it almost certainly will be if you installed it from the repository on any recent (16.04+) Ubuntu version, you can check if it's running using

systemctl status tomcat9.service

(replacing "9" with whatever version you're using) and visually inspecting the output for the Active: field, which will be active (running) if Tomcat is up or inactive (dead) if Tomcat is down.

This is also easy to script, since the command systemctl status tomcat9.service does not require sudo and returns an exit code of 0 if Tomcat is running, or 3 if it is stopped:

# Run the command and suppress the standard output:
systemctl status tomcat9.service > /dev/null 2>&1

# `$?` stores the exit code of the last command,
# which is what we want:
if [ $? -ne 0 ]; then
    # Tomcat is down, do whatever.
fi

If you want to restart Tomcat in the event it's down, that's a bit harder to automate, since the command sudo systemctl (start|restart) tomcat9.service requires sudo. That's a different question, though.

Prior to systemd, Tomcat was still installed as a service on Ubuntu as far back as I'm aware. On these systems, the equivalent command is

$ service tomcat9 status

and you can check the exit codes in the same way.

Upvotes: 0

Techflash
Techflash

Reputation: 767

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

Upvotes: 1

Devendra Dora
Devendra Dora

Reputation: 587

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

Upvotes: 7

Raja Anbazhagan
Raja Anbazhagan

Reputation: 4554

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

Upvotes: 0

Michael Goldshteyn
Michael Goldshteyn

Reputation: 74360

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.

Upvotes: 15

Alin
Alin

Reputation: 314

Try this command

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

It will return the pid if tomcat is running.

Upvotes: 2

Himanshu Chauhan
Himanshu Chauhan

Reputation: 832

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

Upvotes: 2

Batchu Bhargava
Batchu Bhargava

Reputation: 49

$ 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

Upvotes: 0

Dikla
Dikla

Reputation: 3491

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.

Upvotes: 6

dogbane
dogbane

Reputation: 274592

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" [email protected]
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.

Upvotes: 62

konkit
konkit

Reputation: 349

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.

Upvotes: 6

Kemboi
Kemboi

Reputation: 769

try this instead and because it needs root privileges use sudo

sudo service tomcat7 status

Upvotes: 76

Dhanish Jose
Dhanish Jose

Reputation: 747

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

Upvotes: 13

Lokanath
Lokanath

Reputation: 113

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

Upvotes: 8

Will Glass
Will Glass

Reputation: 4850

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.

Upvotes: 2

Thai Tran
Thai Tran

Reputation: 9935

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

Upvotes: 114

Alex Howansky
Alex Howansky

Reputation: 53563

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.

Upvotes: 4

Related Questions