Monitor the service, alarm, and start the service regularly

Source: Internet
Author: User


Scripting function: Monitor the service process every 5 minutes, this process is 3 Tomcat service, when the service hangs, send alarm mail, and start the service

1: Create a log File script

[email protected] ~]# cat a.sh
#!/bin/bash
B=a. ' Date +%y%m%d-%t '. Log

Echo-n "-------------------------$b"

[Email protected] ~]# sh a.sh
-------------------------A.20150604-18:04:20.log

2: Create a service startup script

#!/bin/bash

B=a. ' Date +%y%m%d-%t '. Log


#sh/opt/apache-tomcat-6.0.35/bin/startup.sh && tail-f/opt/apache-tomcat-6.0.35/logs/catalina.out >> $b &

sh/opt/apache-tomcat-6.0.35/bin/startup.sh >> $b &

Sleep 30

echo "tomcat8081 start ********************************************************************" >> $b &

#sh/opt/apache-tomcat-6.0.35_8082/bin/startup.sh && tail-f/opt/apache-tomcat-6.0.35_8082/logs/ Catalina.out >> $b &

# #如果采用tail log output, all the logs will be entered into this file # #

sh/opt/apache-tomcat-6.0.35_8082/bin/startup.sh >> $b &

Sleep 60

echo "tomcat8082 start over********************************************************************" >> $b &

#sh/opt/apache-tomcat-6.0.35_8083/bin/startup.sh && tail-f/opt/apache-tomcat-6.0.35_8083/logs/ Catalina.out >> $b &

sh/opt/apache-tomcat-6.0.35_8083/bin/startup.sh >> $b &

Sleep 90
echo "tomcat8083 start ********************************************************************" >> $b &

3. Send alert SMS via external SMTP Service

Yum-y Install MAILX

Yum-y Install SendMail

Start the SendMail service.

Service SendMail Start

Configure MAIL.RC External SMTP Service

Set [email protected]
Set smtp=mail.qq.com
Set [email protected]
Set SMTP-AUTH-PASSWORD=QQ
Set Smtp-auth=login

Test echo java stop,please Check | Mail-s "title" [Email protected]

We see that the above Mail has been sent successfully!

4: Modify service startup script, send alarm SMS when service hangs, and start service

Start_java () {

killall-9 Java
B=a. ' Date +%y%m%d-%t '. Log

sh/opt/apache-tomcat-6.0.35/bin/startup.sh && tail-f/opt/apache-tomcat-6.0.35/logs/catalina.out >> $ B &

Sleep 30

echo "tomcat8081 start ********************************************************************" >> $b &

sh/opt/apache-tomcat-6.0.35_8082/bin/startup.sh && tail-f/opt/apache-tomcat-6.0.35_8082/logs/ Catalina.out >> $b &

echo "tomcat8082 start **********************************************************************" >> $b &

sh/opt/apache-tomcat-6.0.35_8083/bin/startup.sh && tail-f/opt/apache-tomcat-6.0.35_8083/logs/ Catalina.out >> $b &

echo "tomcat8083 start **************************************************************************" >> $b &
}


proess_num= ' ps-c Java--no-header|wc-l '

If [$proess _num-ne 3];then
echo "' Hostname= $HOSTNAME ' $IP ' error,please check ' | Mail-s "Wheat-conver" [email protected] [email protected]
Start_java
Fi

5: Add timed Tasks, detect every 5 minutes

Crontab-e

  

Introduction to the use of the 5:mail command

When you use mail to send mail, you must first start the SendMail service.

Mail–s "Mail Subject" –c "CC Address" –b "BCC address"---F sender e-mail address –F sender name < message content to send

Mail-s test [email protected] #第一种方法, you can use the current shell as an editor, finish editing the content ctrl-d end

echo "Mail Content" |mail-s test [email protected] #第二种方法, I used the most, probably like the plumbing for the sake of it

Mail-s test [email protected] < file #第三种方法 to send the contents of the file to the message

Monitor the service, alarm, and start the service regularly

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.