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_tcat.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_tcat.sh
4. Run Shell Script
You can run the shell script with the following command.
$ /home/manage_tcat.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_tcat.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
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.
Hi, My script stop after kill de process.
Show this messages:
“Restarting Tomcat Server”
“Killed”
But not execute the last line “sudo service tomcat start” and Tomcat not start.
Perhaps, in your case, the ‘sudo kill -9 $pid’ is not fully executed before running ‘sudo service tomcat start’. Try adding a delay of 5 seconds after the kill command and before tomcat start, as shown below. This will make sure that tomcat start is called only after kill command.
if [ -n “$pid” ]
then
{
sudo kill -9 $pid
}
fi
delay 5
sudo service tomcat start
Rename the script “manage_tomcat.sh” to something else without “tomcat”
The script will commit suicide (kill itself) because it kill all process with the keyword “tomcat” including this script
Thank you so much for the tip. We have updated the post accordingly to name the script manage_tcat.sh instead of manage_tomcat.sh