Linux shell attempts to write enterprise-class startup scripts

Source: Internet
Author: User

Enterprise Shell face question 10: Developing an enterprise MySQL startup script

Description

The MySQL startup command is:

1 /bin/shmysqld_safe --pid-file=$mysqld_pid_file_path 2>&1 >/dev/null&

The Stop command logic script is:

123456 < Code class= "Bash Plain" >mysqld_pid= ' cat   "$mysqld _pid_file_path" if   ( kill  -0  $mysqld _pid 2> /dev/null &NBSP;&NBSP; then &NBSP;&NBSP;&NBSP;&NBSP; kill   $mysqld _pid &NBSP;&NBSP;&NBSP;&NBSP; sleep  2 fi

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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475767778798081828384858687888990919293949596 #!/bin/bash# chkconfig: 2345 64 36# description: MySQL startup#Author:oldboy#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"#此步对开机启动及定时启动及其关键。exportPATHreturn_value=0# Lock directory.lockdir=‘/var/lock/subsys‘lock_file_path="$lockdir/mysql" log_success_msg(){     echo" SUCCESS! [email protected]"# 注意函数的缩进,下同,也是专业的表现,可放到functions里。}   log_failure_msg(){         echo" ERROR! [email protected]"}  # Start Funcstart(){    # Start daemon    echo"Starting MySQL"    iftest-x $bindir/mysqld_safe# 启动文件是否可执行。    then        $bindir/mysqld_safe--datadir="$datadir"--pid-file="$mysqld_pid_file_path">/dev/null&        return_value=$? # 是否处理好返回值是区别脚本是否专业规范的关键。        sleep2        # Make lock for CentOS        iftest-w "$lockdir"# 锁目录是否可写。        then            touch"$lock_file_path"# 创建锁文件。        fi        exit$return_value    else        log_failure_msg "Couldn‘t find MySQL server ($bindir/mysqld_safe)"    fi}# Stop Funcstop(){    iftest-s "$mysqld_pid_file_path"# 是否PID文件存在并大小大于0。    then        mysqld_pid=`cat"$mysqld_pid_file_path"`        if(kill-0 $mysqld_pid 2>/dev/null# 检查PID对应的进程是否存在。        then            echo"Shutting down MySQL"            kill$mysqld_pid  # 不能带-9,否则后果自负。            return_value=$?            sleep2        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        iftest-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"$1"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."           exit1        fi        ;;     *)        echo"Usage: $0  {start|stop|restart}"        exit1esacexit$return_value #是否处理好返回值是区别脚本是否专业规范的关键。

Retained original source: http://oldboy.blog.51cto.com/2561410/1945183

Thanks to the author for sharing,

Linux shell attempts to write enterprise-level startup scripts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.