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