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 = "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