Shell Script Detection Service

Source: Internet
Author: User

Lab Environment:

System: centos7-3.10.0-x86_64

Testing Services: apache-tomcat-9.0.7

Experimental reasons:

Because the current environment for the Java test environment, so did not monitor, but recently found that the service is automatically down, and temporarily did not troubleshoot the cause of downtime, so in order to facilitate the use of the test environment, today simply wrote a small script to detect the Tomcat service, in the normal operation of the situation, Will output the log tomcat is running, and will output Tomcat is restart and restart the Tomcat service in case of an abnormal outage.

Lab steps: 1. Create a script that detects if the Tomcat service is running correctly

#!/bin/bash
PS aux | Grep-v grep | grep Java >/dev/null
M=$?
if [[$M-eq 0]]; then
Echo-e "\033[32m Tomcat is running \033[0m"
Date
Else
Echo-e "\033[31m Tomcat is restart \033[0m"
/usr/local/tomcat/bin/startup.sh >/dev/null
Date
Fi

2. Give script execution permission

chmod +x tomcat.sh

3. Create a Scheduled Task 1) to see if the installation is local

Rpm-qa | grep crontab

2) Install the crontab command

Yum-y Install Crontabs

3) Start crontab service

/bin/systemctl Start Crond.service

4) set to start

/bin/systemctl Enable Crond.service

5) Create a scheduled task

Create a scheduled task Crontab-e

View Scheduled Tasks Crontab-l

Note: The above scheduled task executes the script once per minute, which is written in order to specify the time at which the script executes, including running the service or restarting the service, as a log output for future management and troubleshooting.

4, verify the Scheduled task verification method: 1) Check the log for output

Cat/var/log/tomcat.log

2) Turn off the Tomcat service verification log output



Note: If you have previously generated a log, you can turn the service off first, delete the log, and then verify it again.

3) Turn on Tomcat service verification log output



Note: If you have previously generated a log, you can turn the service on first, delete the log, and then verify it again.

Shell Script Detection Service

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.