企业实战题7:
开发mysql多实例启动脚本:
已知mysql多实例启动命令为:mysqld_safe–defaults-file=/data/3306/my.cnf &
停止命令为:mysqladmin -u root -poldboy123 -S /data/3306/mysql.sockshutdown
请完成mysql多实例启动启动脚本的编写
要求:用函数,case语句、if语句等实现。
答案:
[root@clc-server ~]# cat shell7.sh #!/bin/bash ###################### #this scripts is created by clc ####################### # chkconfig: 2345 69 39 # description:manager multiplemysqld server. . /etc/init.d/functions port=3306 mysql_user=root mysql_pwd="oldboy123" cmd_path="/application/mysql/bin" mysql_sock="/data/$port/mysql.sock" function mysql_start(){ if [ ! -e $mysql_sock ] then $cmd_path/mysqld_safe --defaults-file=/data/${port}/my.cnf &>/dev/null & val=$? if [ $val -eq 0 ] then action "Start MySQL..." /bin/true else action "Start MySQL..." /bin/false fi else echo "MySQL is started ...." exit 2 fi } function mysql_stop(){ if [ ! -e $mysql_sock ] then echo "MySQL is stopped..." exit 2 else $cmd_path/mysqladmin -u${mysql_user} -p${mysql_pwd} -S $mysql_sock shutdown if [ $? -eq 0 ] then action "Stop MySQL..." /bin/true else action "Stop MySQL..." /bin/false fi fi } function mysql_status(){ if [ ! -e $mysql_sock ] then echo "MySQL is not running!!!" else echo "MySQL is running!!!" fi } case $1 in start) mysql_start ;; stop) mysql_stop ;; status) mysql_status ;; restart) mysql_stop sleep 2 mysql_start ;; *) printf "Usage: /data/${port}/mysql {start|stop|restart|status}\n" esac [root@clc-server ~]#
别人的答案:
[root@shell scripts]
# vi mysqld1.sh
#!/bin/bash
# chkconfig: 2345 65 37
# description: manager multiplemysqld server.
# Warning: This script uses the/etc/init.d/functions system function.
# System kernel version isCentOS6.6,2.6.32-504.el6.x86_64.
# I'm not sure whether other systemscan be used normally.
# Source function library.
.
/etc/init
.d
/functions
Port=$2
datadir=
/data/
$2
Path=
"/application/mysql"
CmdPath=
"$Path/bin"
MysqlCmd=
"$CmdPath/mysqld_safe"
prog=mysqld
Port=$2
datadir=
/data/
$2
pidfile=${PIDFILE-$datadir
/mysqld
.pid}
lockfile=${LOCKFILE-
/var/lock/subsys/mysqld
$Port}
MysqlConf=
"$datadir/my.cnf"
RETVAL=0
Usage1(){
echo
-e
"\033[33m USAGE: $0 {startport|stop port|status port|restart port} \033[0m"
exit
6
}
Usage2(){
echo
-e
"\033[33m MySQL DaemonProgram Need Configuration File,like $datadir/my.cnf \033[0m"
exit
6
}
[ -n
"$Port"
-a -z
"`echo"
${Port
//
[0-9]/}
"`"
] || Usage1
start(){
[ -x $MysqlCmd ] ||
exit
5
[ -f $MysqlConf ] || Usage2
echo
-n $
"Starting $prog: "
daemon --pidfile=${pidfile}
"$MysqlCmd --defaults-file=$MysqlConf &>/dev/null &"
sleep
1
RETVAL=$?
echo
[ $RETVAL = 0 ] &&
touch
${lockfile}
return
$RETVAL
}
stop(){
echo
-n $
"Stopping $prog: "
killproc -p $pidfile
retval=$?
echo
[ $retval -
eq
0 ] &&
rm
-f${lockfile}
return
$retval
}
restart() {
stop
sleep
1
start
}
rh_status() {
status -p $pidfile
}
rh_status_q() {
rh_status >
/dev/null
2>&1
}
case
"$1"
in
start)
rh_status_q &&
exit
0
$1
;;
stop)
rh_status_q ||
exit
0
$1
;;
restart)
$1
;;
status)
rh_status
;;
*)
Usage1
exit
2
esac
原文链接:20道shell第7题,转载请注明来源!