企业级通过shell脚本开发MySQL启动脚本 案例

企业Shell面试题10:开发MysqL启动脚本
说明
MysqL启动命令为:
/bin/shMysqLd_safe--pid-file=$MysqLd_pid_file_path2>&1>/dev/null&
停止命令为:
MysqLd_pid=`cat"$MysqLd_pid_file_path"`
if(kill-0$MysqLd_pid2>/dev/null)
then
kill$MysqLd_pid
sleep2
fi
请完成MysqL启动脚本的编写
要求:用函数,case语句、if语句等实现。
解答:



[root@db02scripts]#cat/etc/init.d/oldgirl
#!/bin/bash
#chkconfig:23456436
#description:MysqLstartup
#Author:oldboy
#Blog:

spacer.gif

http://oldboy.blog.51cto.com
#Time:2017-07-0709:24:34
#Name:

spacer.gif

MysqLd.sh
#Version:V1.0
#Description:Thisisatestscript.
[-f/etc/init.d/functions]&&source/etc/init.d/functions
Port=3306
User="root"
Bindir="/application/MysqL/bin"
Datadir="/application/MysqL/data"
MysqLd_pid_file_path="/application/MysqL/`hostname`.pid"
PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
exportPATH
return_value=0


#Lockdirectory.
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/MysqL"

log_success_msg(){
echo"SUCCESS!$@"
}
log_failure_msg(){
echo"ERROR!$@"
}



case"$1"in
start)
#Startdaemon
echo"StartingMysqL"
iftest-x$Bindir/MysqLd_safe
then
$Bindir/MysqLd_safe--datadir="$Datadir"--pid-file="$MysqLd_pid_file_path">/dev/null&
return_value=$?
sleep2

#MakelockforCentOS
iftest-w"$lockdir"
then
touch"$lock_file_path"
fi
exit$return_value
else
log_failure_msg"Couldn'tfindMysqLserver($bindir/MysqLd_safe)"
fi
;;
stop)
iftest-s"$MysqLd_pid_file_path"
then
MysqLd_pid=`cat"$MysqLd_pid_file_path"`

if(kill-0$MysqLd_pid2>/dev/null)
then
echo"ShuttingdownMysqL"
kill$MysqLd_pid
return_value=$?
sleep2
else
log_failure_msg"MysqLserverprocess#$MysqLd_pidisnotrunning!"
rm-f"$MysqLd_pid_file_path"
fi
#DeletelockforCentOS
iftest-f"$lock_file_path"
then
rm-f"$lock_file_path"
fi
exit$return_value
else
log_failure_msg"MysqLserverPIDfileCouldnotbefound!"
fi
;;
restart)
if$0stop;then
$0start
else
log_failure_msg"Failedtostoprunningserver,sorefusingtotrytostart."
exit1
fi
;;

*)
echo"Usage:$0{start|stop|restart}"
exit1
;;
esac
exit$return_value














函数版:

#!/bin/bash
#chkconfig:23456436
#description:MysqLstartup
#Author:oldboy
#Blog:

spacer.gif

http://oldboy.blog.51cto.com
#Time:2017-07-0709:24:34
#Name:

spacer.gif

MysqLd.sh#Version:V1.0#Description:Thisisatestscript.[-f/etc/init.d/functions]&&source/etc/init.d/functionsport=3306user="root"bindir="/application/MysqL/bin"datadir="/application/MysqL/data"MysqLd_pid_file_path="/application/MysqL/`hostname`.pid"PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"exportPATHreturn_value=0#Lockdirectory.lockdir='/var/lock/subsys'lock_file_path="$lockdir/MysqL"log_success_msg(){echo"SUCCESS!$@"}log_failure_msg(){echo"ERROR!$@"}start(){#Startdaemonecho"StartingMysqL"iftest-x$bindir/MysqLd_safethen$bindir/MysqLd_safe--datadir="$datadir"--pid-file="$MysqLd_pid_file_path">/dev/null&return_value=$?sleep2#MakelockforCentOSiftest-w"$lockdir"thentouch"$lock_file_path"fiexit$return_valueelselog_failure_msg"Couldn'tfindMysqLserver($bindir/MysqLd_safe)"fi}stop(){iftest-s"$MysqLd_pid_file_path"thenMysqLd_pid=`cat"$MysqLd_pid_file_path"`if(kill-0$MysqLd_pid2>/dev/null)thenecho"ShuttingdownMysqL"kill$MysqLd_pidreturn_value=$?sleep2elselog_failure_msg"MysqLserverprocess#$MysqLd_pidisnotrunning!"rm-f"$MysqLd_pid_file_path"fi#DeletelockforCentOSiftest-f"$lock_file_path"thenrm-f"$lock_file_path"fiexit$return_valueelselog_failure_msg"MysqLserverPIDfileCouldnotbefound!"fi}case"$1"instart)start;;stop)stop;;restart)if$0stop;then$0startelselog_failure_msg"Failedtostoprunningserver,sorefusingtotrytostart."exit1fi;;*)echo"Usage:$0{start|stop|restart}"exit1;;esacexit$return_value

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...