Shell script-Used to check memcache and automatically restart the effective script

Source: Internet
Author: User
Tags memcached

#!/bin/sh#check memcache process and restart if downmm_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_por ts[@]}t=$ (date-d "Today" + "%y-%m-%d%h:%m:%s") i=0while [$i-lt $mm _count]domm_exists= ' Ps-ef|grep "memcached" |grep "${m m_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}f Ilet I++done
===============================================================

Add for automatic execution:
#crontab-E
*/10 * * * */bin/sh/home/xxx/memcached_check.sh

The system automatically executes every 10 minutes memcached_check.sh

-------------------------------------------------------------
Alternate Shell statement:

#检查mysql状态
Port= ' Netstat-na | grep "LISTEN" | grep "3306" | awk ' {print $4} ' | Awk-f. ' {print $} '
If ["$PORT"-eq "3306"]

#检查mysql占CPU负载
Mysql_cpu= ' Top-u root-b-N 1 | grep MySQL | awk ' {print $ *} ' |awk-f. ' {print $} '
#如果mysql CPU load is greater than 80, restart MySQL
If ["$mysql _cpu"-ge "80"]

#获得相关信息
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 $} '

Myport= ' Netstat-na|grep "tcp" |grep "3306" |awk-f[: ""]+ ' {print $} '
haport= ' Netstat-na|grep "UDP" |grep "694" |awk-f[: ""]+ ' {print $} '
Ping= ' ping-c 5 Www.linuxtone.org|awk-F, '/packets/{print $ i} ' |cut-c 2-|awk ' {print '} '
db1ip= ' Ifconfig eth0|awk '/inet/{print $ ' |cut-c 6-'

----------------------------------------------------------------------------
Some information about the shell if statement:
–b return True when file exists and is a block file
-C Returns True when file exists and is a character
-D returns True when pathname exists and is a directory
-E Returns True when a file or directory specified by pathname is present
-F Returns True when file exists and is regular
-G returns True when the file or directory specified by pathname is present and the Sgid bit is set
-H Returns True when file exists and is a symbolic link file, this option is not valid on some old systems
-K returns True when a file or directory specified by pathname exists and the "sticky" bit is set
-P Returns True when file exists and is a command pipeline
-R Returns True when the file or directory specified by pathname is present and readable
-s when file size is larger than 0 o'clock returns true
-U returns True when the file or directory specified by pathname is present and the SUID bit is set
-W Returns True when the file or directory specified by pathname exists and is executable. A directory must be executable for its content to be accessed.
-O Returns True when the file or directory specified by pathname is present and is owned by the user specified by the active user ID of the current process.

UNIX Shell comparison character notation:
-eq equals
-ne Not equal to
-GT Greater than
-lt less than
-le less than or equal to
-ge greater than or equal to
-Z Empty string
= Two characters equal
! = Two characters unequal
-N Non-empty string

------------------------------------------------------------
Some information about crontab:

Basic format:
* * * * * command
Time-sharing Weekly command

The 1th column represents the minute 1~59 per minute, denoted by * or */1, */10 means every 10 minutes
The 2nd column represents the hour 1~23 (0 means 0 points)
The 3rd column represents the date 1~31
The 4th column represents the month 1~12
5th Column Identification Number Week 0~6 (0 = Sunday)

Shell script-Used to check memcache and automatically restart the effective script

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.