[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/functions
if [ "$1" == "start" ];then
action "nginx starting." /bin/true
elif [ "$1" == "stop" ];then
action "nginx is stopped." /bin/true
else
action "nginx start" /bin/false
fi
测试
[root@master4 day5]# sh test_startup.sh
nginx start [FAILED]
[root@master4 day5]# sh test_startup.sh start
nginx starting. [ OK ]
[root@master4 day5]# sh test_startup.sh stop
nginx is stopped. [ OK ]
[root@master4 day5]#
提示输入参数:
#!/bin/sh
# Source function library.
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
if [ "$1" == "start" ];then
action "nginx starting." /bin/true
elif [ "$1" == "stop" ];then
action "nginx is stopped." /bin/true
else
echo "USAGE:$0 {START|STOIP}"
#action "nginx start" /bin/false
exit
fi
判断里面的值
[root@master4 day5]# vim judge.sh
#!/bin/sh
. /etc/init.d/functions
if [ `cat a.log | wc -L` -ne 1 ];then
action "a.log" /bin/false
else
action "a.log" /bin/true
fi
测试:
[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/functions
for f in `ls *.log`
do
if [ `cat $f | wc -L` -ne 1 ];then
action "$f" /bin/false
else
action "$f" /bin/true
fi
done
[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/functions
for 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
fi
done
项目上线的时候,记录所有网页文件的内容长度:
[root@master4 day5]# for f in *.html;do wc -L $f;done > c.txt
[root@master4 day5]# cat c.txt
4 a.html
10 b.html
[root@master4 day5]# cat judge.sh
#!/bin/sh
. /etc/init.d/functions
for 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
fi
done
[root@master4 day5]# sh judge.sh
a.html [ OK ]
b.html [ OK ]
a.建立指纹库
[root@master4 day5]# md5sum * > zhiwen.log
d8e8fca2dc0f896fd7cb4cb0031ba249 a.html
d8e8fca2dc0f896fd7cb4cb0031ba249 a.log
d4790652b48d41469dd26dd97c71a894 b.html
d4790652b48d41469dd26dd97c71a894 b.log
fac9b0bb2ed61fbda507fcbd18184f85 c.txt
47465a40feb7a298f96909e397b8008a judge.sh
b4da16b2efbef8d5800be2693c58457b test_startup.sh
修改文件:
[root@master4 day5]# md5sum -c zhiwen.log
a.html: OK
a.log: OK
b.html: OK
b.log: OK
c.txt: OK
judge.sh: OK
test_startup.sh: OK
[root@master4 day5]# echo oldboy > b.log
[root@master4 day5]# md5sum -c zhiwen.log
a.html: OK
a.log: OK
b.html: OK
b.log: FAILED
c.txt: OK
judge.sh: OK
test_startup.sh: OK
md5sum: WARNING: 1 computed checksum did NOT match
b.实例脚本:
[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.log
md5sum -c md5zhiwen.log | grep -i ‘FAILED‘ > error.log
if [ `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 match
oldboy/1.txt: FAILED
oldboy/3.txt: FAILED
c.脚本2
vim :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/bash
CHECK_DIR=/server/scripts/day5
if [ -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
fi
fi
测试:
[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 80
Trying 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 Connected
Connected to 10.201.106.134.
Connection closed by foreign host.
[root@master4 ~]# echo -e "\n" | telnet 10.201.106.134 80 | grep Connected | wc -l
Connection closed by foreign host.
1
[root@master4 ~]# nmap 10.201.106.134 -p 80 | grep open
80/tcp open http
[root@master4 ~]# nmap 10.201.106.134 -p 80 | grep open | wc -l
1
[root@master4 ~]#
[root@node3 ~]# nc -w 2 10.201.106.134 80 && echo ok || echo no
ok
[root@node3 ~]# nc -w 2 10.201.106.134 80 && echo ok || echo no
no
[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.1
read -p "Please input a number:" ans
case "$ans" in
1)
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."
exit
esac
测试
[root@master4 day5]# sh case-1.sh
Please input a number:1
the num you input is 1
[root@master4 day5]# sh case-1.sh
Please input a number:2
the num you input is 2
[root@master4 day5]# sh case-1.sh
Please input a number:8
the num you input is 8
[root@master4 day5]# cat case-if.sh
#!/bin/sh
read -p "please input a number:" ans
if [ $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"
exit
fi
[root@master4 day5]# cat case-2.sh
#!/bin/bash
# xsz
# e-mail:666@qq.com
# qq:666
# function:case example
# version:1.1
# color defined
RED_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 :" ans
case "$ans" in
apple | 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/sh
RED_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_SUCCESS
echo ---oldboy trainning--- && $SETCOLOR_FAILURE
echo ---oldboy trainning--- && $SETCOLOR_WARNING
echo ---oldboy trainning--- && $SETCOLOR_NORMAL
在脚本命令行传2个参数,给指定内容(第一个参数)加指定颜色(第二个颜色)
[root@master4 day5]# cat color4.sh
#!/bin/bash
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 color {red|yellow|blue|green|pink}"
exit
;;
esac
改造成read脚本:
[root@master4 day5]# cat color-read.sh
#!/bin/bash
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‘
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" in
red|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/bash
oldboy() {
echo "I am oldboy."
}
oldboy
[root@master4 day5]# sh func01.sh
I am oldboy.
[root@master4 day5]#
[root@master4 day5]# cat func01.sh
#!/bin/bash
oldboy() {
echo "I am oldboy."
}
[root@master4 day5]# cat exec.sh
. ./func01.sh
oldboy
[root@master4 day5]# sh exec.sh
I am oldboy.
[root@master4 day5]#
[root@master4 day5]# cat color-test.sh
#!/bin/bash
echo_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 blue
echo_chars xsz yellow
echo_chars xsz green
echo_chars xsz red
echo_chars xsz xxue
[root@master4 day5]# rsync --daemon
[root@master4 day5]#
[root@master4 day5]# ps -ef | grep rsync
root 6884 1 0 23:32 ? 00:00:00 rsync --daemon
[root@master4 day5]# lsof -i :873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 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.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /va/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 10.201.106.0/24
#hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets 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.password
rsync_bakup:rsync1234
启动脚本:
[root@master4 day5]# cat rsyncd
#!/bin/bash
case "$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 start
rsync started successful
[root@master4 day5]# sh rsyncd stop
rsync stopped successful
[root@master4 day5]# sh rsyncd restart
rsync restarted successful
[root@master4 day5]# sh rsyncd status
rsync is running,pid(8987)
上面脚本改进:
[root@master4 day5]# cat rsyncd02
#!/bin/bash
. /etc/init.d/functions
case "$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 start
rsync started [ OK ]
[root@master4 day5]# sh rsyncd start
rsync is staring
[root@master4 day5]#
[root@master4 day5]# sh rsyncd restart
rsync stopped [ OK ]
rsync restarted [ OK ]
继续改进:
[root@node1 ~]# cat /etc/init.d/rsd
#!/bin/bash
. /etc/init.d/functions
start () {
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}"
;;
esac
exit $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 start
rsync started [ OK ]
[root@node1 ~]# /etc/init.d/rsd start
rsyncd running.
[root@node1 ~]# /etc/init.d/rsd start
rsyncd running.
[root@node1 ~]# /etc/init.d/rsd stop
rsync stopped [ OK ]
[root@node1 ~]# /etc/init.d/rsd start
rsync started [ OK ]
[root@node1 ~]# /etc/init.d/rsd restart
rsync stopped [ OK ]
rsync started [ OK ]
[root@node1 ~]# /etc/init.d/rsd status
rsync is running,pid(11520)
[root@node1 ~]#
测试:
[root@node1 ~]# /etc/init.d/rsd status
rsync is running,pid(11701)
[root@node1 ~]# /etc/init.d/rsd stop
rsync stopped [ OK ]
[root@node1 ~]# /etc/init.d/rsd start
rsync started [ OK ]
[root@node1 ~]# /etc/init.d/rsd status
rsync is running,pid(11762)
[root@node1 ~]# /etc/init.d/rsd restart
rsync stopped [ OK ]
rsync started [ OK ]
[root@node1 ~]# /etc/init.d/rsd status
rsync 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 rsd
rsd 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 rsd
rsd 0:off 1:off 2:on 3:on 4:on 5:off 6:off
测试:
[root@node1 ~]# service rsd status
rsync is running,pid(11804)
[root@node1 ~]# service rsd stop
rsync stopped [ OK ]
[root@node1 ~]# service rsd start
rsync started [ OK ]
[root@node1 ~]# service rsd status
rsync is running,pid(11980)
[root@node1 ~]#
[root@node1 ~]#
[root@node1 ~]# service rsd restart
rsync stopped [ OK ]
rsync started [ OK ]
[root@node4 3306]# cat my.cnf
[mysqld]
datadir=/data/3306/
socket=/data/3306/mysql.sock
user=mysql
symbolic-links=0
port=3306
innodb_file_per_table= 1
[mysqld_safe]
log-error=/data/3306/mysqld.log
pid-file=/data/3306/mysqld.pid
[root@node4 3307]# cat my.cnf
[mysqld]
datadir=/data/3307/
socket=/data/3307/mysql.sock
user=mysql
symbolic-links=0
port=3307
innodb_file_per_table= 1
[mysqld_safe]
log-error=/data/3307/mysqld.log
pid-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 mysql
tcp 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
########################
#init
port=3306
mysql_user="root"
mysql_pwd="test3306"
CmdPath="/usr/bin"
mysql_sock="/data/${port}/mysql.sock"
#startup function
function_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 function
function_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 function
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 1
function_start_mysql
}
case "$1" in
start)
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 stop
Stoping MySQL...
[root@node4 3306]# ./mysql3306 start
Starting MySQL...
[root@node4 3306]# ./mysql3306 restart
Restarting 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
########################
#init
port=3306
mysql_user="root"
mysql_pwd="test3306"
CmdPath="/usr/bin"
mysql_sock="/data/${port}/mysql.sock"
#startup function
function_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 function
function_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 function
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 1
function_start_mysql
}
case "$1" in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage: /data/${port}/mysql3306 {start|stop|restart}\n"
esac
原文地址:http://blog.51cto.com/zhongle21/2087739