shell script to restart tomcat server

Shell Script To Start & Restart Tomcat Automatically in Linux

Tomcat is a popular web application container for Java-based websites & applications. It is widely used for almost every Android app. While managing Java-based websites & applications, you will need to start, stop & restart Tomcat server often. In this article, we will look at how to create shell script to start, stop & restart Tomcat automatically in Linux.


Shell Script To Start & Restart Tomcat Automatically in Linux

Here are the steps to create a shell script to start, stop & restart tomcat automatically.


1. Create Shell Script

Open terminal and run the following command to create shell script.

$ sudo vi /home/manage_tomcat.sh


2. Add shell script to restart tomcat automatically

Add the following lines to start, stop & restart tomcat automatically.

#!/bin/sh

echo "Restarting Tomcat Server"
sudo service tomcat stop

pid=`ps aux | grep tomcat | grep -v grep | grep -v retomcat | awk '{print $2}'`
if [ -n "$pid" ]
   then
    {
      sudo kill -9 $pid
    }
fi

sudo service tomcat start

Save and close the file. The above code basically calls stop command for Tomcat service. Sometimes this may not stop tomcat service so we look for Tomcat among running services, using grep command, and get its PID using awk command. Then we issue kill command to kill this service. Finally we call start command for Tomcat service.

We use this approach because sometimes, calling restart command for Tomcat may not work. Tomcat is known to face performance issues due to memory leaks, and interactive queries. So it is advisable to double check if Tomcat is still running after you issue stop command to its service, and manually kill the process if it is still running.


3. Make Shell Script Executable

Run the following command to make shell script executable.

$ sudo chmod +x /home/manage_tomcat.sh


4. Run Shell Script

You can run the shell script with the following command.

$ /home/manage_tomcat.sh
Restarting Tomcat Server


5. Create Cronjob

It is a good practice to regularly restart Tomcat to avoid performance issues and free up memory. You can set up a simple cronjob that runs the above shell script once a week to restart Tomcat automatically, thereby ensuring proper performance.

For this purpose, open crontab with the following command.

$ sudo crontab -e

Add the following line to create a cron job that will run our shell script every Saturday at 10.a.m. In other words, this cron jobs will automatically restart our server every saturday.

0 10 * * 6 /home/manage_tomcat.sh >/dev/null 2>&1

Save and close the file. In the above code, we mute the execution output by directing it to /dev/null.

In this article, we have learnt how to automatically restart Tomcat. You can use it to regularly restart your Tomcat server and keep its performance healthy.

Also read:

Shell Script to Automatically Backup MySQL Database
Htaccess Redirect URL if it contains String or Word
How to Change Root Password in MySQL
How to Run Shell Script as Cron Job
How to Create Cron Job Using Shell Script

Leave a Reply

Your email address will not be published. Required fields are marked *