博客
关于我
shell(十)case的几个典型应用
阅读量:376 次
发布时间:2019-03-05

本文共 3868 字,大约阅读时间需要 12 分钟。

服务启动脚本编写实践

一、服务启动脚本的编写说明

本文将通过case语句,编写不同服务的启动脚本,并集成开机自启功能。以下是具体实现方法。


二、服务启动脚本的案例

1. Nginx服务启动脚本
#!/bin/bash# 定义服务名称和脚本所在目录SERVICENAME="nginx"SCRIPTDIR="/usr/local/lnmp/nginx/sbin"# 检查是否存在关键库文件if [ -f /etc/init.d/functions ]; then    source /etc/init.d/functionsfi# 定义进程判断文件PIDFILE="/usr/local/lnmp/nginx/logs/nginx.pid"# 定义功能模块start_Nginx() {    if [ -f $PIDFILE ]; then        echo "Nginx is running."    else        echo "Nginx is starting."        $SCRIPTDIR/nginx > /dev/null        /bin/true    fi}stop_Nginx() {    if [ -f $PIDFILE ]; then        echo "Nginx is stopping."        $SCRIPTDIR/nginx -s stop > /dev/null        /bin/true    else        echo "Nginx is already stopped."        /bin/true    fi}restart_Nginx() {    if [ -f $PIDFILE ]; then        echo "Nginx is restarting."        $SCRIPTDIR/nginx -s reopen > /dev/null        /bin/true    else        echo "Failed to start Nginx."        /bin/false    fi}reload_Nginx() {    if [ -f $PIDFILE ]; then        echo "Nginx is reloading."        $SCRIPTDIR/nginx -s reload > /dev/null        /bin/true    else        echo "Failed to reload Nginx."        /bin/false    fi}# case语句实现命令解析case $1 in    start)        start_Nginx        RETVAL=$?        ;;    stop)        stop_Nginx        RETVAL=$?        ;;    restart)        restart_Nginx        RETVAL=$?        ;;    reload)        reload_Nginx        RETVAL=$?        ;;    *)        echo "Usage: $0 {start|stop|restart|reload}"        exit 1    ;;esacexit $RETVAL
2. MySQL服务启动脚本
#!/bin/bash# 定义服务名称和脚本所在目录SERVICENAME="mysql"SCRIPTDIR="/usr/local/mysql/bin"# 定义功能模块start_Mysql() {    if [ -f $PIDFILE ]; then        echo "MySQL is running."    else        echo "MySQL is starting."        $SCRIPTDIR/mysql.server start > /dev/null        /bin/true    fi}stop_Mysql() {    if [ -f $PIDFILE ]; then        echo "MySQL is stopping."        $SCRIPTDIR/mysql.server stop > /dev/null        /bin/true    else        echo "MySQL is already stopped."        /bin/true    fi}restart_Mysql() {    if [ -f $PIDFILE ]; then        echo "MySQL is restarting."        $SCRIPTDIR/mysql.server restart > /dev/null        /bin/true    else        echo "Failed to start MySQL."        /bin/false    fi}# case语句实现命令解析case $1 in    start)        start_Mysql        RETVAL=$?        ;;    stop)        stop_Mysql        RETVAL=$?        ;;    restart)        restart_Mysql        RETVAL=$?        ;;    reload)        echo "MySQL does not support reload."        exit 1        ;;    *)        echo "Usage: $0 {start|stop|restart}"        exit 1    ;;esacexit $RETVAL
3. rsync服务启动脚本
#!/bin/bash# 定义服务名称和脚本所在目录SERVICENAME="rsync"SCRIPTDIR="/usr/bin"# 定义功能模块start_Rsync() {    echo "Rsync is starting."    $SCRIPTDIR/rsyncd --daemon > /dev/null    /bin/true}stop_Rsync() {    echo "Rsync is stopping."    $SCRIPTDIR/rsyncd --stop > /dev/null    /bin/true}# case语句实现命令解析case $1 in    start)        start_Rsync        RETVAL=$?        ;;    stop)        stop_Rsync        RETVAL=$?        ;;    *)        echo "Usage: $0 {start|stop}"        exit 1    ;;esacexit $RETVAL

三、服务自启动配置

  • Nginx服务自启动配置

    • 打开终端执行:
      chmod a+x /etc/init.d/nginx
    • 使用chkconfig添加服务:
      chkconfig --add nginxchkconfig --list nginx
    • 设置开机自启动:
      chkconfig nginx on
  • MySQL服务自启动配置

    • 打开终端执行:
      chmod a+x /etc/init.d/mysql
    • 使用chkconfig添加服务:
      chkconfig --add mysqlchkconfig --list mysql
    • 设置开机自启动:
      chkconfig mysql on
  • Rsync服务自启动配置

    • 打开终端执行:
      chmod a+x /etc/init.d/rsync
    • 使用chkconfig添加服务:
      chkconfig --add rsyncchkconfig --list rsync
    • 设置开机自启动:
      chkconfig rsync on

  • 四、运行级别管理

  • 运行级别文件结构

    • 每个服务需要在对应的运行级别文件中添加注释:
      chkconfig: 2345 32 62description: Nginx is a http server!
    • 检查服务状态:
      ll /etc/rc.d/rc3.d/ | grep nginx
  • 启动脚本优先级

    • 优先级越高的启动脚本,越靠前执行:
      ll /etc/rc.d/rc3.d/ | grep 32

  • 五、常用命令

  • Nginx服务管理

    • 查看服务状态:
      chkconfig --list nginx
    • 禁用自启动:
      chkconfig nginx off
  • MySQL服务管理

    • 删除服务:
      chkconfig --del mysql
  • Rsync服务管理

    • 启用服务:
      chkconfig rsync on

  • 通过以上方法,可以轻松编写不同服务的启动关闭脚本,并实现开机自启动功能。

    转载地址:http://lcwwz.baihongyu.com/

    你可能感兴趣的文章
    mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
    查看>>
    MYSQL 查看最大连接数和修改最大连接数
    查看>>
    MySQL 查看有哪些表
    查看>>
    mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
    查看>>
    MySql 查询以逗号分隔的字符串的方法(正则)
    查看>>
    MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
    查看>>
    mysql 查询,正数降序排序,负数升序排序
    查看>>
    MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
    查看>>
    mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
    查看>>
    mysql 死锁(先delete 后insert)日志分析
    查看>>
    MySQL 死锁了,怎么办?
    查看>>
    MySQL 深度分页性能急剧下降,该如何优化?
    查看>>
    MySQL 深度分页性能急剧下降,该如何优化?
    查看>>
    MySQL 添加列,修改列,删除列
    查看>>
    mysql 添加索引
    查看>>
    MySQL 添加索引,删除索引及其用法
    查看>>
    MySQL 用 limit 为什么会影响性能?
    查看>>
    MySQL 用 limit 为什么会影响性能?有什么优化方案?
    查看>>
    MySQL 用户权限管理:授权、撤销、密码更新和用户删除(图文解析)
    查看>>
    mysql 用户管理和权限设置
    查看>>