Old Boys educate Linux high-end Viban shell after class will be exam questions:
Enterprise Shell face question 10: Developing an enterprise MySQL startup script
Description
The MySQL startup command is:
/bin/sh mysqld_safe--pid-file= $mysqld _pid_file_path 2>&1 >/dev/null &
The Stop command logic script is:
Mysqld_pid= ' cat ' $mysqld _pid_file_path "' if (kill-0 $mysqld _pid 2>/dev/null) then kill $mysqld _pid Sleep 2fi
Please complete the MySQL startup script and implement the Chkconfig configuration to boot from.
Requirements: Implemented with functions, case statements, if statements, and so on.
Answer: The technique of this problem is suitable for most startup scripts, for example: Rsync,nginx and so on, only take MySQL as an example to introduce ideas.
Simple, easy-to-use, efficient, professional
#!/bin/bash# chkconfig: 2345 64 36# description: mysql startup#author:o Ldboy#blog:http://oldboy.blog.51cto.com#time:2017-07-07 09:24:34#name:mysqld#version:v1.0#description: This is a test script. [ -f /etc/init.d/functions ] && source /etc/init.d/functionsbindir=]/ Application/mysql/bin "datadir="/application/mysql/data "mysqld_pid_file_path="/application/mysql/' hostname '. PID "Path="/sbin:/usr/sbin:/bin:/usr/bin: $basedir/bin " #此步对开机启动及定时启动及其关键. Export pathreturn_value=0# lock directory.lockdir= '/var/lock/subsys ' lock_file_path= "$lockdir/ MySQL "log_success_msg () { echo " success! [email protected] " # note the indentation of functions, the same below, but also professional performance, can be put into the functions. } log_failure_msg () { echo " Error! [email protected] "} # start funcstart () { # Start daemon echo "Starting mysql" if test -x $bindir/mysqld_safe # Whether the startup file can be executed. then $bindir/mysqld_safe --datadir = "$datadir" --pid-file= "$mysqld _pid_file_path" >/dev/null & return_value=$? # whether the return value is handled is the key to distinguishing whether the script is professional. sleep 2 # make lock for centos if test -w "$lockdir" # lock directory is writable. then touch "$lock _file_path" # create lock file. fi exit $return _value else log_failure_msg "Couldn ' t find mysql server ($bindir/mysqld_ Safe) " fi}# stop funcstop () { if test -s "$mysqld _pid_file_path" # whether the PID file exists and the size is greater than 0. then mysqld_pid= ' cat ' $mysqld _pid_ File_path "' if (kill -0 $mysqld _pid 2>/ Dev/null) # Check whether the PID corresponding process exists. then echo "Shutting down mysql" kill $mysqld _pid # can not take-9, otherwise the consequences of self-esteem. return_value=$? sleep 2 else log_failure_msg "mysql server process # $mysqld _pid is not running!" rm -f "$mysqld _pid_file_path " fi # Delete lock for oldboy ' s centos if test -f "$lock _file_path" then rm -f "$lock _file_path" fi exit $return _value else log_failure_msg "mysql server pid file could not be found! " fi}case "$" in start) start ;; stop) stop ;; restart) if $0 stop; then $0 start else log_failure_msg "failed to stop running server, so refusing to try to Start. " exit 1 fi ;; *) echo "usage: $0 {start|stop|restart} " exit 1esacexit $return _ value #是否处理好返回值是区别脚本是否专业规范的关键.
More shell knowledge can refer to the old boy's new book "Learn Linux with the old boy operations: Shell programming Practice", the major bookstores are sold
Https://item.jd.com/12117874.html
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/9B/28/wKiom1le9rrDj6u4AAFVi8WAzKU935.png "title=" a.png "alt=" Wkiom1le9rrdj6u4aafvi8wazku935.png "/>
Let's set a small goal, proficient in shell programming! If proficient, then 15K, you can contact the old boy to recommend business for you.
This article is from the "Old Boys Linux Training" blog, make sure to keep this source http://oldboy.blog.51cto.com/2561410/1945183
Developing Enterprise standard service startup script case (MySQL) through shell