Use shell scripts in CentOS to monitor whether the website is normal

Source: Internet
Author: User
Recently I bought a vps and put a website on it. when a large volume of traffic is always unresponsive, I wrote a monitoring script to check the website every minute. if the website cannot be accessed, I restarted the service. 1. write a Shell script and save it to/opt/http_monitor #! /Bin/bash # website url URL & quot; http://www.xxx.com/recently bought a vps, put a website on it, a lot of access is always no response, I wrote a monitoring script, the service is detected once every minute and restarted if it cannot be accessed.

1. write a Shell script and save it to/opt/http_monitor.
#! /Bin/bash

# Website url
URL = "http://www.xxx.com /"

# Obtain the http response code
HTTP_CODE = 'curl-o/dev/null-s-w "% {http_code}" "$ {URL }"'
# Echo $ HTTP_CODE

# If the server can respond normally, 200 of the code should be returned.
If [$ HTTP_CODE! = 200]; then
# Restart the service
Service httpd restart
Fi

2. add to crontab and use the crontab-e command to add a new task:

Shell code
# Run once every minute
*/1 */opt/http_monitor

3. stop the service, test, cool! Started automatically one minute later! However, the Chinese characters are garbled. after checking that the system variables were not loaded, you can add the locale environment variable to http_monitor:

Shell code

Export LC_ALL = zh_CN.UTF-8
 
Related Article

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.