美文网首页
centos编译安装后的nginx启动脚本

centos编译安装后的nginx启动脚本

作者: hsyman | 来源:发表于2019-07-30 15:47 被阅读0次

    编译安装的nginx默认是无法使用service nginx start
    或者systemctl start nginx管理进程
    为了方便使用,有时候需要自己编写启动脚本
    vi /etc/init.d/nginx 写入以下内容:

    #!/bin/bash
    # wobuchimangguo
    # chkconfig: - 85 15   这一行必须要
    # description: nginx init tools  这一行必须要
    
    conf=/opt/nginx/conf/nginx.conf
    bin=/opt/nginx/sbin/nginx
    pid=/opt/nginx/logs/nginx.pid
    #以上内容根据实际内容填写
    
    SHOW () {
        if [ $? -eq 0 ];then
            echo -en "$1 [\e[1;32mOK\e[0m]";echo
        else
            echo -en "$1 [\e[1;31mFAIL\e[0m]";echo
        fi
    }
    
    START () {
        if [ -f $pid ];then
            echo "Nginx is running,please use [stop|restart|reload|status]"
        else
            $bin && SHOW 'Start nginx'
        fi
    }
    
    STOP () {
        if [ -f $pid ];then
            kill `cat $pid` && SHOW 'Stop nginx'
        else
            echo "Nginx is not running,please use [start|restart|status]"
        fi
    }
    
    RESTART () {
        kill `cat $pid` && SHOW 'Stop nginx'
        $bin && SHOW 'Start nginx'
    }
    
    RELOAD () {
        if [ -f $pid ];then
            $bin -s reload && SHOW 'Reload nginx'
        else
            echo "Nginx is not running,please use [start|status]"
        fi
    }
    
    STATUS () {
        if [ -f $pid ];then
            echo -en "Nginx is \e[1;32mrunning\e[0m";echo
        else
            echo -en "Nginx is \e[1;31mdeal\e[0m";echo
        fi
    }
    
    case $1 in
        start) START;;
        stop) STOP;;
        restart) RESTART;;
        reload) RELOAD;;
        status) STATUS;;
        *) 
            echo "Please use [start|stop|restart|reload|status]" && exit 1
        ;;
    esac
    

    填写完毕以后
    chmod +x /etc/init.d/nginx
    chkconfig --add /etc/init.d/nginx
    chkconfig nginx on
    完毕!试试命令service nginx status或者systemctl status nginx

    相关文章

      网友评论

          本文标题:centos编译安装后的nginx启动脚本

          本文链接:https://www.haomeiwen.com/subject/chenrctx.html