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

前端之家收集整理的这篇文章主要介绍了企业级通过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

原文链接:https://www.f2er.com/bash/392212.html

猜你在找的Bash相关文章