Shell script checks the memcache process and automatically restarts

Source: Internet
Author: User

Shell script checks the memcache process and automatically restarts
Revised version:

#! /Bin/sh
# Check memcache process and restart if down

Mm_bin = "/usr/local/bin/memcached"
Mm_log = "/home/xxx/memcached_check.log"
Mm_ports = ("11211" "11212 ")
Mm_param = ("-d-m 20480-p 11211-u www" "-d-m 256-p 11212-u www ")
Mm_count =$ {# mm_ports [@]}

T = $ (date-d "today" + "% Y-% m-% d % H: % M: % S ")
I = 0
While [$ I-lt $ mm_count]
Do
Mm_exists = 'ps-ef | grep "memcached" | grep "$ {mm_ports [$ I]}" | grep-v grep | wc-l'

If ["$ mm_exists" = "0"]; then
$ {Mm_bin }$ {mm_param [$ I]} 2> & 1>/dev/null &
Echo "$ {t }:: {mm_bin }$ {mm_param [$ I]}" >$ {mm_log}
Fi

Let I ++
Done

========================================================== ======================================

Add as automatic execution:
# Crontab-e
*/10 */bin/sh/home/xxx/memcached_check.sh


The system automatically executes memcached_check.sh every 10 minutes.

-------------------------------------------------------------
Backup shell statement:

# Check the mysql status
PORT = 'netstat-na | grep "LISTEN" | grep "3306" | awk '{print $4}' | awk-F. '{print $2 }''
If ["$ PORT"-eq "3306"]

# Check the CPU usage of mysql
Mysql_cpu = 'top-U root-B-n 1 | grep mysql | awk '{print $10}' | awk-F. '{print $1 }''
# If the mysql cpu load is greater than 80, restart mysql.
If ["$ mysql_cpu"-ge "80"]

# Obtain relevant information
Ps-ef | grep "memcached" | grep-v "grep" | wc-l
Ps-ef | grep "memcached" | grep "11211" | grep-v "grep" | wc-l
Ps aux | grep "memcached" | grep-v "grep" | awk '{sum + = $4; n ++}; END {print sum }'
Ps aux | grep "memcached" | grep-v "grep" | awk '{printf $1 }'

MYPORT = 'netstat-na | grep "tcp" | grep "3306" | awk-F [: ""] + '{print $5 }''
HAPORT = 'netstat-na | grep "udp" | grep "694" | awk-F [: ""] + '{print $5 }''
PING = 'Ping-c 5 www.linuxtone.org | awk-F, '/packets/{print $3}' | cut-c 2-| awk' {print $1 }''
DB1IP = 'ifconfig eth0 | awk '/inet/{print $2}' | cut-c 6 -'

----------------------------------------------------------------------------
Shell if statement:
-B. returns true if the file exists and is a block file.
-C: returns true if the file exists and is a character file.
-D. If pathname exists and is a directory, true is returned.
-E: returns true if the file or directory specified by pathname exists.
-F returns true if the file exists and is a regular file
-G returns true if the file or directory specified by pathname exists and the SGID bit is set.
-H returns true if the file exists and is a symbolic link file. This option is invalid in some old systems.
-K. If a file or directory specified by pathname exists and a "Sticky" bit is set, the system returns the true value.
-P: returns true if the file exists and is a command pipeline.
-R: returns true if the file or directory specified by pathname exists and is readable.
-S: returns true if the file size is greater than 0.
-U returns true if a file or directory specified by pathname exists and SUID is set.
-W returns true if the file or directory specified by pathname exists and is executable. A directory must be executable for its content access.
-O returns true if a file or directory specified by pathname exists and the user specified by the valid user ID of the current process is owned by it.

Comparison character writing in UNIX Shell:
-Eq equals
-Ne is not equal
-Gt greater
-Lt is less
-Le is less than or equal
-Ge is greater than or equal
-Z empty string
= Two equal characters
! = Two characters
-N non-empty string

------------------------------------------------------------
Crontab information:

Basic Format:
* *** Command
Hour, day, month, and week commands


The 1st column indicates minute 1 ~ 59. Each minute is represented by * or */1, and */10 indicates every 10 minutes.
The first column indicates the hour 1 ~ 23 (0 indicates 0 points)
The 3rd column indicates the date 1 ~ 31
The 4th column indicates the month 1 ~ 12
The Identification Number of column 5th is from day of the week to day ~ 6 (0 indicates Sunday)

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.