本文共 22424 字,大约阅读时间需要 74 分钟。
[root@master4 ~]# ll /etc/init.d/functions -rw-r--r--. 1 root root 13948 Sep 16 2015 /etc/init.d/functions[root@master4 ~]#
[root@master4 day5]# vim test_startup.sh#!/bin/sh# Source function library.[ -f /etc/init.d/functions ] && . /etc/init.d/functionsif [ "$1" == "start" ];then action "nginx starting." /bin/trueelif [ "$1" == "stop" ];then action "nginx is stopped." /bin/trueelse action "nginx start" /bin/falsefi测试[root@master4 day5]# sh test_startup.sh nginx start [FAILED][root@master4 day5]# sh test_startup.sh startnginx starting. [ OK ][root@master4 day5]# sh test_startup.sh stopnginx is stopped. [ OK ][root@master4 day5]# 提示输入参数:#!/bin/sh# Source function library.[ -f /etc/init.d/functions ] && . /etc/init.d/functionsif [ "$1" == "start" ];then action "nginx starting." /bin/trueelif [ "$1" == "stop" ];then action "nginx is stopped." /bin/trueelse echo "USAGE:$0 {START|STOIP}" #action "nginx start" /bin/false exitfi
判断里面的值[root@master4 day5]# vim judge.sh #!/bin/sh. /etc/init.d/functionsif [ `cat a.log | wc -L` -ne 1 ];then action "a.log" /bin/falseelse action "a.log" /bin/truefi测试:[root@master4 day5]# sh judge.sh a.log [ OK ][root@master4 day5]# echo 111 > a.log [root@master4 day5]# sh judge.sh a.log [FAILED][root@master4 day5]#多文件测试:[root@master4 day5]# vim judge.sh #!/bin/sh. /etc/init.d/functionsfor f in `ls *.log`do if [ `cat $f | wc -L` -ne 1 ];then action "$f" /bin/false else action "$f" /bin/true fidone[root@master4 day5]# sh judge.sh a.log [FAILED]b.log [ OK ][root@master4 day5]# 记住默认长度:[root@master4 day5]# for f in `ls *.log`;do wc -L $f;done > c.txt[root@master4 day5]# cat judge.sh #!/bin/sh. /etc/init.d/functionsfor f in `ls *.log`do if [ `cat $f | wc -L` -ne `grep $f c.txt | awk '{print $1}'` ];then action "$f" /bin/false else action "$f" /bin/true fidone
项目上线的时候,记录所有网页文件的内容长度:[root@master4 day5]# for f in *.html;do wc -L $f;done > c.txt [root@master4 day5]# cat c.txt 4 a.html10 b.html[root@master4 day5]# cat judge.sh #!/bin/sh. /etc/init.d/functionsfor f in `ls *.html`do if [ `cat $f | wc -L` -ne `grep $f c.txt | awk '{print $1}'` ];then action "$f" /bin/false else action "$f" /bin/true fidone[root@master4 day5]# sh judge.sh a.html [ OK ]b.html [ OK ]
a.建立指纹库[root@master4 day5]# md5sum * > zhiwen.logd8e8fca2dc0f896fd7cb4cb0031ba249 a.htmld8e8fca2dc0f896fd7cb4cb0031ba249 a.logd4790652b48d41469dd26dd97c71a894 b.htmld4790652b48d41469dd26dd97c71a894 b.logfac9b0bb2ed61fbda507fcbd18184f85 c.txt47465a40feb7a298f96909e397b8008a judge.shb4da16b2efbef8d5800be2693c58457b test_startup.sh修改文件:[root@master4 day5]# md5sum -c zhiwen.log a.html: OKa.log: OKb.html: OKb.log: OKc.txt: OKjudge.sh: OKtest_startup.sh: OK[root@master4 day5]# echo oldboy > b.log[root@master4 day5]# md5sum -c zhiwen.log a.html: OKa.log: OKb.html: OKb.log: FAILEDc.txt: OKjudge.sh: OKtest_startup.sh: OKmd5sum: WARNING: 1 computed checksum did NOT matchb.实例脚本:[root@master4 day5]# mkdir oldboy[root@master4 day5]# touch oldboy/{1..9}.txt[root@master4 day5]# ls oldboy/1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt[root@master4 day5]# vim md5check01.sh #!/bin/sh. /etc/init.d/functions[root@master4 day5]# vim md5check01.sh #!/bin/sh[ ! -f md5zhiwen.log ] && md5sum oldboy/* > md5zhiwen.logmd5sum -c md5zhiwen.log | grep -i 'FAILED' > error.logif [ `cat error.log | wc -l` -ge 1 ];then echo "`cat error.log`"fi测试:[root@master4 day5]# echo 3 > oldboy/3.txt [root@master4 day5]# [root@master4 day5]# [root@master4 day5]# sh md5check01.sh md5sum: WARNING: 2 computed checksums did NOT matcholdboy/1.txt: FAILEDoldboy/3.txt: FAILEDc.脚本2vim :g/ggg/s//root/g生成指纹库:[root@master4 day5]# find /server/scripts/day5/ -type f -name "*" -exec md5sum {}>/root/checkmd5.db \;[root@master4 day5]# vim md5check02.sh #!/bin/bashCHECK_DIR=/server/scripts/day5if [ -e /root/checkmd5.db ];then md5sum -c /root/checkmd5.db > /root/result.db 2>/dev/null egrep -i "FAILED" /root/result.db > /root/err.log if [ -s /root/err.log ];then mail -s "`uname -n ` $(date +%F) web is error" root@localhost < /root/err.log else echo "file is not modife" | mail -s "web is true" root@localhost fifi测试:[root@master4 day5]# echo "djfldjfld" > a.log [root@master4 day5]# sh -x md5check02.sh + CHECK_DIR=/server/scripts/day5+ '[' -e /root/checkmd5.db ']'+ md5sum -c /root/checkmd5.db+ egrep -i FAILED /root/result.db+ '[' -s /root/err.log ']'++ uname -n++ date +%F+ mail -s 'master4.com 2018-01-14 web is error' root@localhost放到计划任务:[root@master4 day5]# crontab -e#md5 check etc***** /bin/sh /server/scripts/day5/md5check02.sh > /dev/null 2>&1
[root@master4 ~]# telnet 10.201.106.134 80Trying 10.201.106.134...Connected to 10.201.106.134.Escape character is '^]'.[root@master4 ~]# echo -e "\n" | telnet 10.201.106.134 80 | grep ConnectedConnected to 10.201.106.134.Connection closed by foreign host.[root@master4 ~]# echo -e "\n" | telnet 10.201.106.134 80 | grep Connected | wc -lConnection closed by foreign host.1
[root@master4 ~]# nmap 10.201.106.134 -p 80 | grep open80/tcp open http[root@master4 ~]# nmap 10.201.106.134 -p 80 | grep open | wc -l1[root@master4 ~]#
[root@node3 ~]# nc -w 2 10.201.106.134 80 && echo ok || echo nook[root@node3 ~]# nc -w 2 10.201.106.134 80 && echo ok || echo nono[root@node3 ~]#
http://oldboy.blog.51cto.com/2561410/942530
[root@master4 day5]# cat case-1.sh #!/bin/bash# this script is created by xsz# e-mail:635254246@qq.com# qqinfo:666# version:1.1read -p "Please input a number:" anscase "$ans" in1) echo "the num you input is 1";;2) echo "the num you input is 2";;[3-9]) echo "the num you input is $ans";;*) echo "the num you input must be less 9." exitesac测试[root@master4 day5]# sh case-1.shPlease input a number:1the num you input is 1[root@master4 day5]# sh case-1.shPlease input a number:2the num you input is 2[root@master4 day5]# sh case-1.shPlease input a number:8the num you input is 8[root@master4 day5]# cat case-if.sh #!/bin/shread -p "please input a number:" ansif [ $ans -eq 1 ];then echo "the num you input is 1" elif [ $ans -eq 2 ];then echo "the num your input 2"elif [ $ans -ge 3 -a $ans -le 9 ];then echo "the num you input is $ans"else echo "the num you input must be less 9" exitfi
[root@master4 day5]# cat case-2.sh #!/bin/bash# xsz# e-mail:666@qq.com# qq:666# function:case example# version:1.1# color definedRED_COLOR='\E[1;31m'GREEN_COLOR='\E[1;32m'YELLOW_COLOR='\E[1;33m'BLUE_COLOR='\E[1;34m'RES='\E[0m'read -p "Please input the fruit name you like :" anscase "$ans" inapple | APPLE) echo -e "the fruit name you like is ${RED_COLOR}"$ans."${RES}";;banana | BANANA) echo -e "the fruit name you like is ${YELLOW_COLOR}$ans.${RES}";;pear | PEAR) echo -e "the fruit name you like is ${GREEN_COLOR}"$ans."${RES}";;*) echo -e "Here is not the fruit name you like--${BLUE_COLOR}"$ans."${RES}" exit;;esac
[root@master4 day5]# cat color.sh echo -e "\033[30m 黑色字 oldboy trainning \033[0m"echo -e "\033[31m 红色字 oldboy trainning \033[0m"echo -e "\033[32m 绿色字 oldboy trainning \033[0m"echo -e "\033[33m ×××字 oldboy trainning \033[0m"echo -e "\033[34m 蓝色字 oldboy trainning \033[0m"echo -e "\033[35m 紫色字 oldboy trainning \033[0m"echo -e "\033[36m 天蓝字 oldboy trainning \033[0m"echo -e "\033[37m 白色字 oldboy trainning \033[0m"
[root@master4 day5]# cat color2.sh #!/bin/shRED_COLOR='\E[1;31m'GREEN_COLOR='\E[1;32m'YELLOW_COLOR='\E[1;33m'BLUE_COLOR='\E[1;34m'PINK='\E[1;35m'RES='\E[0m'echo -e "${RED_COLOR}===red color===${RES}"echo -e "${YELLOW_COLOR}===yellow color===${RES}"echo -e "${BLUE_COLOR}===blue color===${RES}"echo -e "${GREEN_COLOR}===green color===${RES}"echo -e "${PINK}===pink color===${RES}"[root@master4 day5]# [root@master4 day5]# sh color2.sh ===red color======yellow color======blue color======green color======pink color===[root@master4 day5]#
[root@master4 day5]# cat color3.sh SETCOLOR_SUCCESS="echo -en \\033[1;32m"SETCOLOR_FAILURE="echo -en \\033[1;31m"SETCOLOR_WARNING="echo -en \\033[1;33m"SETCOLOR_NORMAL="echo -en \\033[1;39m"echo ---oldboy trainning--- && $SETCOLOR_SUCCESSecho ---oldboy trainning--- && $SETCOLOR_FAILUREecho ---oldboy trainning--- && $SETCOLOR_WARNINGecho ---oldboy trainning--- && $SETCOLOR_NORMAL
在脚本命令行传2个参数,给指定内容(第一个参数)加指定颜色(第二个颜色)[root@master4 day5]# cat color4.sh #!/bin/bashRED_COLOR='\E[1;31m'GREEN_COLOR='\E[1;32m'YELLOW_COLOR='\E[1;33m'BLUE_COLOR='\E[1;34m'PINK_COLOR='\E[1;35m'RES='\E[0m'if [ $# -ne 2 ];then echo "Usage $0 content {red|yellow|blue|green|pink}" exitficase "$2" inred|RED) echo -e "${RED_COLOR}$1${RES}" ;;yellow|YELLOW) echo -e "${YELLOW_COLOR}$1${RES}" ;;green|GREEN) echo -e "${GREEN_COLOR}$1${RES}" ;;blue|BLUE) echo -e "${BLUE_COLOR}$1${RES}" ;;pink|PINK) echo -e "${PINK_COLOR}$1${RES}" ;;*) echo "Usage color {red|yellow|blue|green|pink}" exit ;;esac改造成read脚本:[root@master4 day5]# cat color-read.sh #!/bin/bashRED_COLOR='\E[1;31m'GREEN_COLOR='\E[1;32m'YELLOW_COLOR='\E[1;33m'BLUE_COLOR='\E[1;34m'PINK_COLOR='\E[1;35m'RES='\E[0m'read -t 9 -p "Please input tow char,format:content color:" content color[ -z "$content" -o -z "$color" ] && { echo "Usage $0 content {red|yellow|blue|green|pink}" exit}case "$color" inred|RED) echo -e "${RED_COLOR}$color${RES}" ;;yellow|YELLOW) echo -e "${YELLOW_COLOR}$color${RES}" ;;green|GREEN) echo -e "${GREEN_COLOR}$color${RES}" ;;blue|BLUE) echo -e "${BLUE_COLOR}$color${RES}" ;;pink|PINK) echo -e "${PINK_COLOR}$color${RES}" ;;*) echo "Usage color {red|yellow|blue|green|pink}" exit ;;esac
[root@master4 day5]# cat color5.sh echo -e "\033[40;37m 黑底白字 welcome \033[0m"echo -e "\033[41;37m 红底白字 welcome \033[0m"echo -e "\033[42;37m 绿底白字 welcome \033[0m"echo -e "\033[43;37m 黄底白字 welcome \033[0m"echo -e "\033[44;37m 蓝底白字 welcome \033[0m"echo -e "\033[45;37m 紫底白字 welcome \033[0m"echo -e "\033[46;37m 天蓝白字 welcome \033[0m"echo -e "\033[47;30m 白底黑字 welcome \033[0m"[root@master4 day5]#
[root@master4 day5]# cat func01.sh #!/bin/basholdboy() { echo "I am oldboy."}oldboy[root@master4 day5]# sh func01.sh I am oldboy.[root@master4 day5]#
[root@master4 day5]# cat func01.sh #!/bin/basholdboy() { echo "I am oldboy."}[root@master4 day5]# cat exec.sh . ./func01.sholdboy[root@master4 day5]# sh exec.sh I am oldboy.[root@master4 day5]#
[root@master4 day5]# cat color-test.sh #!/bin/bashecho_chars() { RED_COLOR='\E[1;31m' GREEN_COLOR='\E[1;32m' YELLOW_COLOR='\E[1;33m' BLUE_COLOR='\E[1;34m' PINK_COLOR='\E[1;35m' RES='\E[0m' if [ $# -ne 2 ];then echo "Usage $0 content {red|yellow|blue|green|pink}" exit fi case "$2" in red|RED) echo -e "${RED_COLOR}$1${RES}" ;; yellow|YELLOW) echo -e "${YELLOW_COLOR}$1${RES}" ;; green|GREEN) echo -e "${GREEN_COLOR}$1${RES}" ;; blue|BLUE) echo -e "${BLUE_COLOR}$1${RES}" ;; pink|PINK) echo -e "${PINK_COLOR}$1${RES}" ;; *) echo "Usage 1 {red|yellow|blue|green|pink}" exit ;; esac}echo_chars xsz blueecho_chars xsz yellowecho_chars xsz greenecho_chars xsz redecho_chars xsz xxue
[root@master4 day5]# rsync --daemon[root@master4 day5]# [root@master4 day5]# ps -ef | grep rsyncroot 6884 1 0 23:32 ? 00:00:00 rsync --daemon[root@master4 day5]# lsof -i :873COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMErsync 6884 root 4u IPv4 304578 0t0 TCP *:rsync (LISTEN)rsync 6884 root 5u IPv6 304579 0t0 TCP *:rsync (LISTEN)[root@master4 day5]# cat /etc/rsyncd.confuid = rsyncgid = rsyncuse chroot = nomax connections = 200timeout = 300pid file = /var/run/rsyncd.pidlock file = /va/run/rsync.locklog file = /var/log/rsyncd.logignore errorsread only = falselist = falsehosts allow = 10.201.106.0/24#hosts deny = 0.0.0.0/32auth users = rsync_backupsecrets file = /etc/rsync.password# rsync_config___________end[oldboy]path = /oldboy/[data]path = /data/[backup]path = /backup/# rsync_config___________end[root@master4 day5]# vim /etc/rsyncd.passwordrsync_bakup:rsync1234启动脚本:[root@master4 day5]# cat rsyncd #!/bin/bashcase "$1" in start) rsync --daemon [ $? -eq 0 ] && echo "rsync started successful";; stop) pkill rsync;sleep 1 [ `ps -ef | grep "rsync --daemon" | grep -v grep | wc -l` -eq 0 ] &&\ echo "rsync stopped successful";; restart) pkill rsync sleep 1 rsync --daemon [ $? -eq 0 ] && echo "rsync restarted successful";; status) pid_status=`lsof -i :873 | awk '{print $2}' | grep [0-9] | uniq` [ -n "$pid_status" ] && echo "rsync is running,pid($pid_status)" || echo "rsync is not running.";; *) echo "Usage:$0 {start|stop|restart}";; esac[root@master4 day5]# sh rsyncd startrsync started successful[root@master4 day5]# sh rsyncd stoprsync stopped successful[root@master4 day5]# sh rsyncd restartrsync restarted successful[root@master4 day5]# sh rsyncd statusrsync is running,pid(8987)
上面脚本改进:[root@master4 day5]# cat rsyncd02 #!/bin/bash. /etc/init.d/functionscase "$1" in start) [ -e /var/run/rsyncd.pid ] && echo "rsync is staring" && exit rsync --daemon if [ $? -eq 0 ];then action "rsync started" /bin/true else action "rsync started" /bin/false fi;; stop) pkill rsync;sleep 1 if [ `ps -ef | grep "rsync --daemon" | grep -v grep | wc -l` -eq 0 ];then action "rsync stopped" /bin/true else action "rsync stopped" /bin/false fi;; restart) pkill rsync sleep 1 if [ `ps -ef | grep "rsync --daemon" | grep -v grep | wc -l` -eq 0 ];then flag=0 action "rsync stopped" /bin/true else action "rsync stopped" /bin/false fi rsync --daemon if [ $? -eq 0 -a $flag -eq 0 ];then action "rsync restarted" /bin/true else action "rsync restarted" /bin/false fi;; status) pid_status=`lsof -i :873 | awk '{print $2}' | grep [0-9] | uniq` [ -n "$pid_status" ] && echo "rsync is running,pid($pid_status)" || echo "rsync is not running.";; *) echo "Usage:$0 {start|stop|restart}";; esac测试:[root@master4 day5]# sh rsyncd startrsync started [ OK ][root@master4 day5]# sh rsyncd startrsync is staring[root@master4 day5]# [root@master4 day5]# sh rsyncd restartrsync stopped [ OK ]rsync restarted [ OK ]
继续改进:[root@node1 ~]# cat /etc/init.d/rsd#!/bin/bash. /etc/init.d/functionsstart () { if [ `ps -ef | grep "rsync --daemon" | grep -v grep | wc -l` -ge 1 ];then echo "rsyncd running." exit fi rsync --daemon if [ $? -eq 0 ];then action "rsync started" /bin/true else action "rsync started" /bin/false fi}stop(){ if [ `ps -ef | grep "rsync --daemon" | grep -v grep | wc -l` -eq 0 ];then echo "rsyncd is stopped." exit fi pkill rsync sleep 1 if [ `ps -ef | grep "rsync --daemon" | grep -v grep | wc -l` -eq 0 ];then action "rsync stopped" /bin/true else action "rsync stopped" /bin/false fi}restart() { if [ `ps -ef | grep "rsync --daemon" | grep -v grep | wc -l` -eq 0 ];then start else stop start fi}status() { pid_status=`lsof -i :873 | awk '{print $2}' | grep [0-9] | uniq` [ -n "$pid_status" ] && echo "rsync is running,pid($pid_status)" || echo "rsync is not running."}case "$1" in start) start RETUVAL=$?;; stop) stop RETUVAL=$?;; restart) restart RETUVAL=$?;; status) status RETUVAL=$?;; *) echo "Usage:$0 {start|stop|restart}";; esacexit $RETUVAL需要改名否则会异常[root@master4 day5]# mv rsyncd rsd给文件增加执行权限:[root@master4 day5]# chmod +x rsd拷贝到启动路径:[root@master4 day5]# cp rsd /etc/init.d/之前的系统环境是centos7,默认调用systemctl需要拷贝脚本和配置到centos6运行测试:[root@node1 ~]# /etc/init.d/rsd startrsync started [ OK ][root@node1 ~]# /etc/init.d/rsd startrsyncd running.[root@node1 ~]# /etc/init.d/rsd startrsyncd running.[root@node1 ~]# /etc/init.d/rsd stoprsync stopped [ OK ][root@node1 ~]# /etc/init.d/rsd startrsync started [ OK ][root@node1 ~]# /etc/init.d/rsd restartrsync stopped [ OK ]rsync started [ OK ][root@node1 ~]# /etc/init.d/rsd statusrsync is running,pid(11520)[root@node1 ~]# 测试:[root@node1 ~]# /etc/init.d/rsd statusrsync is running,pid(11701)[root@node1 ~]# /etc/init.d/rsd stoprsync stopped [ OK ][root@node1 ~]# /etc/init.d/rsd startrsync started [ OK ][root@node1 ~]# /etc/init.d/rsd statusrsync is running,pid(11762)[root@node1 ~]# /etc/init.d/rsd restartrsync stopped [ OK ]rsync started [ OK ][root@node1 ~]# /etc/init.d/rsd statusrsync is running,pid(11804)[root@node1 ~]# 放到系统自启动服务:[root@node1 ~]# vim /etc/init.d/rsd #!/bin/bash. /etc/init.d/functions# chkconfig: 2345 21 81[root@node1 ~]# chkconfig --add rsd[root@node1 ~]# chkconfig --list | grep rsdrsd 0:off 1:off 2:on 3:on 4:on 5:on 6:off[root@node1 ~]# [root@node1 ~]# chkconfig --level 234 rsd on[root@node1 ~]# chkconfig --list rsdrsd 0:off 1:off 2:on 3:on 4:on 5:off 6:off测试:[root@node1 ~]# service rsd statusrsync is running,pid(11804)[root@node1 ~]# service rsd stoprsync stopped [ OK ][root@node1 ~]# service rsd startrsync started [ OK ][root@node1 ~]# service rsd statusrsync is running,pid(11980)[root@node1 ~]# [root@node1 ~]# [root@node1 ~]# service rsd restartrsync stopped [ OK ]rsync started [ OK ]
[root@node4 3306]# cat my.cnf [mysqld]datadir=/data/3306/socket=/data/3306/mysql.sockuser=mysqlsymbolic-links=0port=3306innodb_file_per_table= 1[mysqld_safe]log-error=/data/3306/mysqld.logpid-file=/data/3306/mysqld.pid[root@node4 3307]# cat my.cnf [mysqld]datadir=/data/3307/socket=/data/3307/mysql.sockuser=mysqlsymbolic-links=0port=3307innodb_file_per_table= 1[mysqld_safe]log-error=/data/3307/mysqld.logpid-file=/data/3307/mysqld.pid初始化实例/usb/bin/mysql_install_db --datadir=/data/3306/usb/bin/mysql_install_db --datadir=/data/3307后台运行:[root@node4 3306]# /bin/sh /usr/bin/mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null &[root@node4 3307]# /bin/sh /usr/bin/mysqld_safe --defaults-file=/data/3307/my.cnf 2>&1 > /dev/null &[root@node4 mysql]# netstat -tanp | grep mysqltcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3275/mysqld tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 3428/mysqld [root@node4 mysql]# 脚本1:通过sock判断:[root@node4 3306]# cat mysql3306 #!/bin/sh######################### this scripts is created by xsz at 2018-02-12# xsz QQ:66# function:mysqld start scripts#########################initport=3306mysql_user="root"mysql_pwd="test3306"CmdPath="/usr/bin"mysql_sock="/data/${port}/mysql.sock"#startup functionfunction_start_mysql(){ if [ ! -e "$mysql_sock" ];then printf "Starting MySQL...\n" /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null & else print "MySQL is running...\n" exit fi}#stop functionfunction_stop_mysql(){ if [ ! -e "$mysql_sock" ];then printf "MySQL is stopped...\n" exit else printf "Stoping MySQL...\n" ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown fi}#restart functionfunction_restart_mysql(){ printf "Restarting MySQL...\n" function_stop_mysql sleep 1 function_start_mysql}case "$1" instart) function_start_mysql;;stop) function_stop_mysql;;restart) function_restart_mysql;;*) printf "Usage: /data/${port}/mysql3306 {start|stop|restart}\n"esac[root@node4 3306]# ./mysql3306 stopStoping MySQL...[root@node4 3306]# ./mysql3306 startStarting MySQL...[root@node4 3306]# ./mysql3306 restartRestarting MySQL...Stoping MySQL...Starting MySQL...脚本2:通过端口判断:[root@node4 3306]# cat mysql-port3306 #!/bin/sh######################### this scripts is created by xsz at 2018-02-12# xsz QQ:66# function:mysqld start scripts#########################initport=3306mysql_user="root"mysql_pwd="test3306"CmdPath="/usr/bin"mysql_sock="/data/${port}/mysql.sock"#startup functionfunction_start_mysql(){ if [ `netstat -lnt | grep $port | wc -l` -eq 0 ];then printf "Starting MySQL...\n" /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null & else print "MySQL is running...\n" exit fi}#stop functionfunction_stop_mysql(){ if [ `netstat -lnt | grep $port | wc -l` -eq 0 ];then printf "MySQL is stopped...\n" exit else printf "Stoping MySQL...\n" ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown fi}#restart functionfunction_restart_mysql(){ printf "Restarting MySQL...\n" function_stop_mysql sleep 1 function_start_mysql}case "$1" instart) function_start_mysql;;stop) function_stop_mysql;;restart) function_restart_mysql;;*) printf "Usage: /data/${port}/mysql3306 {start|stop|restart}\n"esac
转载于:https://blog.51cto.com/zhongle21/2087739