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

编译安装的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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。