centos編譯安裝后的nginx啟動腳本

編譯安裝的nginx默認(rèn)是無法使用service nginx start
或者systemctl start nginx管理進(jìn)程
為了方便使用,有時候需要自己編寫啟動腳本
vi /etc/init.d/nginx 寫入以下內(nèi)容:

#!/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
#以上內(nèi)容根據(jù)實(shí)際內(nèi)容填寫

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容