修改版:
#!/bin/bash# chkconfig: 2345 64 36 #配置系统自启动# description: A very fast and reliable SQL database engine.############################################################### File Name: mysqld# Version: V1.0# Created Time : 2018-06-05 08:58:19###############################################################引入系统函数库. /etc/init.d/functions#基础路径定义port=3310basedir='/opt/mysql'bindir='/opt/mysql/bin'mysql_sock="/data/${port}/mysql.sock"lockdir='/var/lock/subsys'lock_file_path="$lockdir/mysql"mysqld_pid_file_path='/data/${port}/`uname -n`.pid'#成功提示函数log_success_msg(){#action为特殊的提示函数,$@为所有参数。action "SUCCESS! $@" /bin/true}#失败提示函数log_failure_msg(){action "ERROR! $@" /bin/false}#mysql启动函数start(){echo $"Starting MySQL"#测试mysqld_safe是否可执行if test -x $bindir/mysqld_safethen#后台执行启动mysql命令$bindir/mysqld_safe &>/dev/null &#获取返回值retval=$?#判断返回值是否为0if [ $retval -eq 0 ]then#调用成功提示函数。log_success_msg "mysql Startuped"if test -w "$lockdir" #判断锁目录是否可写。thentouch "$lock_file_path" #创建锁文件。fireturn $retval #给返回值是专业的表现。elselog_failure_msg "MySQL Startup" #调用失败函数提示。return $retvalfielselog_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"fi}#停止MySQL函数。stop(){#判断mysql pid file大小是否为0。if test -s "$mysqld_pid_file_path"then#读取pidfilemysqld_pid=`cat "$mysqld_pid_file_path"`#判断mysql pid对应的进程是否存在。if (kill -0 $mysqld_pid 2>/dev/null)thenecho $"Shutting down MySQL"kill $mysqld_pid #停止MySQL命令。retval=$?if [ $retval -eq 0 ]thenlog_success_msg "MySQL Stop" #调用停止成功函数。if test -f "$lock_file_path"thenrm -f "$lock_file_path" #删除锁文件。fireturn $retvalelselog_failure_msg "MySQL Stop."return $retvalfielselog_failure_msg "MySQL server process mysqld_pid is not running!"rm "$mysqld_pid_file_path"fielselog_failure_msg "MySQL server PID file is null or not exist!"fi}#接收传参判断并执行相应函数。case "$1" instart)startretval=$?;;stop)stopretval=$?;;restart)stopsleep 2 #这里很重要,要休息一下。startretval=$?;;*)echo $"Usage:$0 {start|stop|restart}"exit 2esacexit $retval #执行脚本后,有返回值才更专业。
原版:
#!/bin/bash# chkconfig: 2345 64 36 #配置系统自启动# description: A very fast and reliable SQL database engine.############################################################### File Name: mysqld# Version: V1.0# Author: oldboy# Organization: www.oldboyedu.com# Created Time : 2018-06-05 08:58:19###############################################################引入系统函数库. /etc/init.d/functions#基础路径定义basedir='/application/mysql'bindir='/application/mysql/bin'lockdir='/var/lock/subsys'lock_file_path="$lockdir/mysql"mysqld_pid_file_path='$basedir/data/`uname -n`.pid'#成功提示函数log_success_msg(){#action为特殊的提示函数,$@为所有参数。action "SUCCESS! $@" /bin/true}#失败提示函数log_failure_msg(){action "ERROR! $@" /bin/false}#mysql启动函数start(){echo $"Starting MySQL"#测试mysqld_safe是否可执行if test -x $bindir/mysqld_safethen#后台执行启动mysql命令$bindir/mysqld_safe &>/dev/null &#获取返回值retval=$?#判断返回值是否为0if [ $retval -eq 0 ]then#调用成功提示函数。log_success_msg "mysql Startup"if test -w "$lockdir" #判断锁目录是否可写。thentouch "$lock_file_path" #创建锁文件。fireturn $retval #给返回值是专业的表现。elselog_failure_msg "MySQL Startup" #调用失败函数提示。return $retvalfielselog_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"fi}#停止MySQL函数。stop(){#判断mysql pid file大小是否为0。if test -s "$mysqld_pid_file_path"then#读取pidfilemysqld_pid=`cat "$mysqld_pid_file_path"`#判断mysql pid对应的进程是否存在。if (kill -0 $mysqld_pid 2>/dev/null)thenecho $"Shutting down MySQL"kill $mysqld_pid #停止MySQL命令。retval=$?if [ $retval -eq 0 ]thenlog_success_msg "MySQL Stop" #调用停止成功函数。if test -f "$lock_file_path"thenrm -f "$lock_file_path" #删除锁文件。fireturn $retvalelselog_failure_msg "MySQL Stop."return $retvalfielselog_failure_msg "MySQL server process mysqld_pid is not running!"rm "$mysqld_pid_file_path"fielselog_failure_msg "MySQL server PID file is null or not exist!"fi}#接收传参判断并执行相应函数。case "$1" instart)startretval=$?;;stop)stopretval=$?;;restart)stopsleep 2 #这里很重要,要休息一下。startretval=$?;;*)echo $"Usage:$0 {start|stop|restart}"exit 2esacexit $retval #执行脚本后,有返回值才更专业。
