Write a script to automatically start Tomcat

Source: Internet
Author: User

My server uses tomcat. From time to time, the Tomcat process will suddenly end, and I don't know why. From the log, there is nothing suspicious. It seems like this is suddenly gone, the following logs are printed after Tomcat is restarted. The cause cannot be found, but you need to find a work und. Do not go to the server to restart tomcat after the problem occurs. I plan to use the cron service of the system to automatically start Tomcat. It is very simple to check the Tomcat process once every minute. If not, start it. The script is as follows:

#!/bin/bashexport JRE_HOME=/usr/local/jreif [ `ps -ef | egrep ‘tomcat‘ | egrep -v egrep | wc -l` -gt 0 ]; thenexit 1fiecho `date`" tomcat restart" >> ~/shell/tomcat_restart.log/usr/local/tomcat/bin/startup.sh

Note the following when writing this script:Do not add spaces!Shell programming is not the same as other programming. spaces have special meanings. When the script starts tomcat, it prints a log ~ /Shell/tomcat_restart.log contains a time information to help me find the cause (although I still cannot find the cause ).

Next, configure crontab.

$ Crontab-e

Then edit the content as (If your shell file is saved in ~ /Shell/check_and_start_server.sh ):

*****~ /Shell/check_and_start_server.sh

Save and exit crontab editing, and then start the cron service:

/Etc/init. d/crond start

In this way, the cron Service performs a check every minute. It takes about 10 seconds to start Tomcat, so the worst case is that your server may be interrupted for more than one minute (considering that the server exited at 14:20:01, the cron service has just been checked. The next check is 14:21:00. At this time, Tomcat will be started, which will be completed around 14:21:10 ).

Write a script to automatically start Tomcat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.