Actual combat simulation monitor MySQL Service shell script summary _linux Shell

Source: Internet
Author: User
Tags sleep

1 Port Judgment ==> only for database local use

Method for judging the 1:if condition of law

[Root@oldboy scripts]# cat check_db01.sh
#!/bin/sh
#created by Oldboy
#mail: oldboy521@gmail.com
portnum= ' Netstat-lnt|grep 3306|wc-l '
if [$PortNum-eq 1]
then
 echo "Mysqld is running."
else
 echo "Mysqld is stoped."
Fi

Method 2: Conditional expression usage

[Root@oldboy scripts]# cat check_db01_1.sh
#!/bin/sh
#created by Oldboy
#mail: oldboy521@gmail.com
 
portnum= ' netstat-lnt|grep 3306|wc-l '
#PortNum = ' nmap 10.0.0.189-p 3306|grep ' open|wc-l '
[$PortNum-eq 1] && echo "Mysqld is running." | | echo "Mysqld is stoped."

Method 3: Using the System function display method

[Root@oldboy scripts]# cat check_db01_2.sh
#!/bin/sh
#created by Oldboy
#mail: oldboy521@gmail.com
# Load functions
[-f/etc/init.d/functions] &&/etc/init.d/functions
portnum= ' Netstat-lnt|grep 3306| Wc-l '
if [$PortNum-eq 1]
then
 action mysqld is running. '/bin/true
else
 action ' mysqld is Stope D. "/bin/false
fi

2 process plus port judgment ==> only for database local use

[Root@oldboy scripts]# cat check_db02.sh
#!/bin/sh
#created by Oldboy
#mail: oldboy521@gmail.com
 
portnum= ' Netstat-lnt|grep 3306|wc-l '
Processnum= ' Ps-ef|grep mysql|grep-v grep| Wc-l '
#echo $PortNum $ProcessNum
#exit 1
 
if [$PortNum-eq 1-a $ProcessNum-eq 2]
then
 echo "M Ysqld is running. "
else
 echo "Mysqld is stoped."
Fi

Tip: If you are not assured, you can also add analog user access.

Tip: Scripting Debugging Tips

1 grep filtered string, do not include in the filename, example: grep mysql, file name for check_mysql.sh is not very good. Better file names such as:
-rw-r--r--1 root 236 Sep 9 22:19 check_db01.sh
-rw-r--r--1 root 293 Sep 9 22:34 check_db02.sh
-rw-r--r--1 root 212 Sep 9 22:49 check_db03.sh
2 when the syntax is no problem but the results are not correct, you can use the following methods to troubleshoot:
(a) Print the variable results after the definition of the variable to see if it is consistent with the actual result, such as:
Echo $PortNum $ProcessNum
Exit 1
b sh-x check_db02.sh Way, tracking script execution process, more debugging scenarios see later shell script debugging.
3 the way to determine whether the remote MySQL service is normal

Method 1: Port check ==> is suitable for use on remote server (local also suitable)

[Root@oldboy scripts]# cat check_db03.sh
#!/bin/sh
#created by Oldboy
#mail: oldboy521@gmail.com
Portnum= ' nmap 10.0.0.189-p 80|grep open|wc-l '
if [$PortNum-eq 1]
then
 echo "Mysqld is running."
else
 echo "Mysqld is stoped."
Fi

Tip: There are a number of ways to use the port, including 3 methods that are not limited to the following address, http://oldboy.blog.51cto.com/2561410/942530
For example: You can also use the Nagios check_tcp Plug-ins and other methods.
Act 2: Simulate user (including Web server) access method ==> is suitable for remote server use (local also appropriate)

[Root@oldboy scripts]# cat check_db04.sh
#!/bin/sh
#created by Oldboy
#mail: oldboy521@gmail.com
 
Mysql-s/data/3306/mysql.sock-e "select version ();" >/dev/null 2>&1
if [$-eq 0]
then
 echo "Mys Qld is running. "
else
 echo "Mysqld is stoped."
Fi

Skills:
1 Use a Web Connection account to simulate access.
Law 3: Simulate the way users use the program URL to judge

<?php
/* #this scripts is created by Oldboy
#oldboy qq:31333741
#site: http:// www.etiantian.org
#blog: http://oldboy.blog.51cto.com
#oldboy trainning QQ group:208160987 226199307 44246017
* *
  $link _id=mysql_connect (' 10.0.0.4 ', ' Oldboy ', ' oldboy123 ') or mysql_error ();
  if ($link _id) {
   echo "MySQL successful by Oldboy!";
  } else{
   Echo mysql_error ();
>

The above method is just a simple judgment, then the following gives a set of judgment, processing, and alarm in a more professional MySQL check processing script.
4) More professional MySQL check processing script solution

#!/bin/bash #created by Oldboy QQ 49000448 #date: 20100918 myuser=root mypass= "Oldboy" Mysock=/data/3306/mysql.sock mysql_startup= "/data/3306/mysql" log_path=/tmp log_file=${log_path}/mysqllogs_ ' Date +%f '. LOG MYSQL_PATH=/usr/local 
/mysql/bin mysql_cmd= "$MYSQL _path/mysql-u$myuser-p$mypass-s $MYSOCK" #→ whole variable definition way, appear more professional. 
$MYSQL _cmd-e "select version ();" >/dev/null 2>&1 if [$-eq 0] then echo "MYSQL is running!" 
Exit 0 Else $MySQL _startup Start > $LOG _file#→ log is also a variable. 
Sleep 5; $MYSQL _cmd-e "select version ();" >/dev/null 2>&1 if [$-ne 0] then for num in ' seq 10 ' #→ kill mysqld through A for loop, 
The real kill then exits the cycle or kills every two seconds, killing 10 times altogether. 
Do killall mysqld>/dev/null 2>&1 [$-ne 0] && break; Sleep 2 $MySQL _startup start >> $LOG _file fi $MYSQL _cmd-e "select version ();" >/dev/null 2>&1 &am p;& status= "restarted" | | 
Status= "Unknown" #→ this logic is more accurate. echo "MySQL status is $Status" >> $LOG _file mail-s "MySQL status is $Status"31333741@qq.com < $LOG _file #→ the above status as the result of the title to the message, of course you can do SMS, voice call alarm."  Fi exit

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.